心情糟糕了几天,唉西~ 好几天没怎么做题
这个题刚开始怎么想也都没有想到是一个搜索题,直接模拟也可以,后来一想,这个问题,是从当前位置出发,只有一种可能性的搜索,以前都是从队列取出当前点后,通过for循环寻找下一个点,这个直接求出下一个点就可以,而且是一定合法的,终止条件稍微考虑一下,我写了一个 4 * len的平方 肯定比实际范围大一些
//leehaoze
#include <iostream>
#include <deque>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
const int INF = 1 << 29;
#define INC_SAT(val) (val = ((val)+1 > (val)) ? (val)+1 : (val))
#define ARR_SIZE(a) ( sizeof( (a) ) / sizeof( (a[0]) ) )
#define ULL unsigned long long
#define MAXN (1000 + 5)
int N, len;
string s1, s2, ans;
struct Chip {
Chip(string d, int s) : data_(d), step_(s) {}
string data_;
int step_;
};
void Input() {
cin >> len >> s1 >> s2 >> ans;
}
string operator+(string s1, string s2) {
string temp = "";
for (int i = 0; i < len; ++i) {
temp += s2[i];
temp += s1[i];
}
return temp;
}
string Shuffle(string s) {
string s1 = "";
string s2 = "";
for (int i = 0; i < len; ++i) {
s1 += s[i];
s2 += s[i + len];
}
return (s1 + s2);
}
void BFS() {
queue<Chip> Q;
Q.push(Chip(s1 + s2, 1));
while (!Q.empty()) {
Chip now = Q.front();
Q.pop();
if (now.data_ == ans) {
cout << now.step_ << endl;
return;
}
if (now.step_ > 4 * len * len) {
cout << -1 << endl;
return;
}
Q.push(Chip(Shuffle(now.data_), now.step_ + 1));
}
}
int main() {
#ifdef LOCAL
freopen("IN.txt", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
Input();
cout << i + 1 << ' ';
BFS();
}
}