q.push(start); while (q.size()) { auto t = q.front(); q.pop();
int x, y; for (int i = 0; i < t.size(); ++ i) if (t[i] == 'x') { x = i / 3, y = i % 3; break; }
for (int i = 0; i < 4; ++ i) { string state = t; int a = x + dx[i], b = y + dy[i]; if (a >= 0 && a < 3 && b >= 0 && b < 3) { swap(state[x * 3 + y], state[a * 3 + b]); if (!map.count(state)) { map[state] = map[t] + 1; if (state == end) return map[state]; q.push(state); } } } }
return-1; }
intmain() { string s; char c; for (int i = 0; i < 9; ++ i) cin >> c, s += c; cout << bfs(s) << endl; return0; }