#include<iostream>#include<algorithm>#include<queue>#include<unordered_map>usingnamespace std;intbfs(string start){//定义目标状态
string end ="12345678x";//定义队列和dist数组
queue<string> q;
unordered_map<string,int> d;//初始化队列和dist数组
q.push(start);
d[start]=0;//转移方式int dx[4]={1,-1,0,0}, dy[4]={0,0,1,-1};while(q.size()){auto t = q.front();
q.pop();//记录当前状态的距离,如果是最终状态则返回距离int distance = d[t];if(t == end)return distance;//查询x在字符串中的下标,然后转换为在矩阵中的坐标int k = t.find('x');int x = k /3, y = k %3;for(int i =0; i <4; i++){//求转移后x的坐标int a = x + dx[i], b = y + dy[i];//当前坐标没有越界if(a >=0&& a <3&& b >=0&& b <3){//转移xswap(t[k], t[a *3+ b]);//如果当前状态是第一次遍历,记录距离,入队if(!d.count(t)){
d[t]= distance +1;
q.push(t);}//还原状态,为下一种转换情况做准备swap(t[k], t[a *3+ b]);}}}//无法转换到目标状态,返回-1return-1;}intmain(){
string c, start;//输入起始状态for(int i =0; i <9; i++){
cin >> c;
start += c;}
cout <<bfs(start)<< endl;return0;}