原文
作者:
huolong
, 时间:2022-01-02 11:25:17 , 所有人可见, 阅读 19
```cpp
#include
#include
#include
#include
using namespace std;
int bfs(string state)
{
queue q;
unordered_map d;
__________
d[state] = 0;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
string end = "12345678x";
while (q.size())
{
auto t = q.front();
q.pop();
__________
int distance = d[t];
int k = t.find('x');
int x = __________, y =__________;
for (int i = 0; i < 4; i ++ )
{
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < 3 && b >= 0 && b < 3)
{
__________;
if (!d.count(t))
{
__________;
q.push(t);
}
swap(t[a * 3 + b], t[k]);
}
}
}
return -1;
}
int main()
{
string state;
char c;
for (int i = 0; i < 9; i ++ )
{
cin >> s;
state += c;
}
cout << bfs(state) << endl;
return 0;
}
```