#include<bits/stdc++.h>
#define int long long
using namespace std;
const int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};//dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};这么改就AC了
int fx,fy;
string ss;
struct matrix
{
int a[5][5];
bool operator< (matrix x) const
{
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
if(a[i][j]!=x.a[i][j]) return a[i][j]<x.a[i][j];
return false;
}
}st,en;
int h(matrix a)
{
int ret=0;
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
if(a.a[i][j]!=st.a[i][j]) ret++;
return ret;
}
struct node
{
matrix a;
int t;
bool operator< (node x) const
{
return t+h(a)>x.t+h(x.a);
}
}x;
priority_queue<node>q;
set<matrix>s;
signed main()
{
st.a[1][1]=1;
st.a[1][2]=2;
st.a[1][3]=3;
st.a[2][1]=8;
st.a[2][2]=0;
st.a[2][3]=4;
st.a[3][1]=7;
st.a[3][2]=6;
st.a[3][3]=5;
cin>>ss;
for(int i=0;i<=8;i++)
en.a[i/3+1][i%3+1]=ss[i]-'0';
q.push({en,0});
while(!q.empty())
{
x=q.top();
q.pop();
if(!h(x.a))
{
printf("%lld",x.t);
return 0;
}
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
if(!x.a.a[i][j]) fx=i,fy=j;
for(int i=0;i<=3;i++)
{
int xx=fx+dx[i],yy=fy+dy[i];
if(xx>=1&&xx<=3&&yy>=1&&yy<=3)
{
swap(x.a.a[fx][fy],x.a.a[xx][yy]);
if(!s.count(x.a))
{
s.insert(x.a);
q.push({x.a,x.t+1});
}
swap(x.a.a[fx][fy],x.a.a[xx][yy]);
}
}
}
return 0;
}
谁能告诉我为什么啊!!