#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<map>
using namespace std;
struct node{
int sx,sy;
};
queue<node> q;
node w,o;
map<string,int> ans;
int dx[4]={1,-1,0,0},
dy[4]={0,0,1,-1};
string s;
int k[4][4];
void _1to2()
{
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
k[i][j]=int(s[(i-1)*3+j-1])-48;
}
void _2to1()
{
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
s[(i-1)*3+j-1]=char(k[i][j]+48);
}
int main()
{
cin>>s;
_1to2();
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
if(k[i][j]==0)
{
w.sx=i,w.sy=j;
break;
}
}
}
q.push(w);
ans[s]=1;
while(!q.empty())
{
w.sx=q.front().sx;
w.sy=q.front().sy;
q.pop();
string t=s;
for(int i=0;i<4;i++)
{
int x=w.sx+dx[i],y=w.sy+dy[i];
if(x<1||x>3||y<1||y>3)
continue;
swap(k[x][y],k[w.sx][w.sy]);
_2to1();
if(!ans[s])
{
ans[s]=ans[t]+1;
t=s;
w.sx=x,w.sy=y;
_1to2();
}
else
{
swap(k[x][y],k[w.sx][w.sy]);
_2to1();
}
}
}
cout<<ans["123804765"]<<endl;
return 0;
}
样例都过不去...