我的代码:
#include <bits/stdc++.h>
using namespace std;
//int dx[] = {-1, 1, 2, 2, 1, -1, -2, -2};
//int dy[] = {2, 2, 1, -1, -2, -2, -1, 1};
int dir[] = {-11, -9, -3, 7, 11, 9, 3, -7};
struct node {
string val;
int step;
node (string _val, int _step) {
val = _val;
step = _step;
}
};
map<string, bool> mp;
char c;
string st, to = "111110111100*110000100000";
bool BFS (int dep) {
mp.clear();
queue<node> q;
q.push(node(st, 0));
mp[st] = true;
while (!q.empty()) {
node nowfront = q.front();
// cout << "***" << nowfront.step << endl;
q.pop();
if (nowfront.step == dep) continue;
string nowval = nowfront.val;
int nowstar = nowval.find('*');
for (int i = 0; i < 8; ++i) {
int nextqs = nowstar + dir[i];
if (nextqs >= 0 && nextqs <= 24) {
string nextval = nowval;
nextval[nextqs] = nowval[nowstar], nextval[nowstar] = nowval[nextqs];
int cnt = 0;
for (int j = 0; j <= 24; ++j) {
if (nextval[j] != to[j]) cnt++;
}
if (nowfront.step + cnt - 1 > dep) continue;
if (mp[nextval] == false) {
mp[nextval] = true;
q.push(node(nextval, nowfront.step + 1));
if (nextval == to) {
// puts("!!!");
return true;
}
}
}
}
}
return false;
}
int main() {
// freopen("knight.in","r",stdin);
// freopen("knight.out","w",stdout);
int T;
cin >> T;
// getchar();
while (T--) {
st = "";
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= 5; ++j) {
cin >> c;
st += c;
}
}
// cout << st.size() << endl;
// cout << st << endl;
// int cnt = 0;
// for (int i = 0; i <= 24; ++i) {
// if (st[i] != to[i]) cnt++;
// }
bool flag = false;
for (int i = 1; i <= 15; ++i) {
// cout << "***" << i << endl;
if (BFS(i) == true) {
cout << i << endl;
flag = true;
break;
}
}
if (flag == false) puts("-1");
}
return 0;
}
/*
2
10110
01*11
10111
01001
00000
01011
110*1
01110
01010
00100
1
00000
10000
11*00
11110
11111
*/
我的输出第二个样例:
01011
110*1
01110
01010
00100
输出-1
,第一个是7的样例是对的,自己怎么改也不知道哪里错了,求大佬开导