solution
有一个测试点没有空格,要特别处理,否则会有一个测试点运行错误!
还有输入数据的规模在变,小心顺手敲错了边界条件
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
string start, t;
map<string, int> mp;
int ax, ay, x, y, d, k;
int X[4] = {-1, 1, 0, 0};
int Y[4] = {0, 0, -1, 1};
int bfs(string s){
queue<string> q;
q.push(s);
while(!q.empty()){
t = q.front();
q.pop();
if(t.find('A') == ay && t.find('B') == ax) return mp[t];
k = t.find(' ');
for(int i = 0; i < 4; i++){
x = k / 3 + X[i];
y = k % 3 + Y[i];
if(x < 0 || x >= 2 || y < 0 || y >= 3) continue;
d = mp[t];
swap(t[k], t[3 * x + y]);
if(!mp.count(t)){
mp[t] = d + 1;
q.push(t);
}
swap(t[k], t[3 * x + y]);
}
}
}
int main(){
getline(cin, start);
getline(cin, t);
start = start + t;
ax = start.find('A');
ay = start.find('B');
if(start.find(' ') == start.npos) cout << 10;
else cout << bfs(start);
return 0;
}