|
为什么RE???- #include<iostream>
- using namespace std;
- string s;
- int i,j,l,head,tail,temp;
- const int mx=100010;
- const int mm=4;
- int dx[mx],dy[mx],d[mx];
- int a[mm][mm][mx];
- int fx[5]= {0,-1,0,0,+1};
- int fy[5]= {0,0,-1,+1,0};
- int mymap[5][5]= {0,0,0,0,0,
- 0,1,2,3,0,
- 0,8,0,4,0,
- 0,7,6,5,0,
- 0,0,0,0,0
- };
- bool bb(int x)
- {
- int k,v;
- for (k=1; k<=3; k++)
- for (v=1; v<=3; v++)
- if (a[k][v][x]!=mymap[k][v]) return false;
- return true;
- }
- void cc(int x)
- {
- int k,v;
- for (k=1; k<=3; k++)
- {
- for (v=1; v<=3; v++)
- cout<<a[k][v][x]<<" ";
- cout<<endl;
- }
- cout<<endl;
- }
- int main()
- {
- cin>>s;
- for (i=0; i<=2; i++)
- for (j=0; j<=2; j++)
- {
- a[i+1][j+1][1]=s[3*i+j]-'0';
- if (a[i+1][j+1][1]==0) dx[1]=i+1,dy[1]=j+1;
- }
- ///cc(1);
- head=1,tail=2,d[1]=0;
- while (head<=tail)
- {
- if (bb(head))
- {
- cout<<d[head];
- return 0;
- }
- int nowx=dx[head];
- int nowy=dy[head];///现在的空位
- for (i=1; i<=4; i++)
- {
- int tx=nowx+fx[i];
- int ty=nowy+fy[i];///移动的位置
- if (1<=tx&&tx<=3&&1<=ty&&ty<=3)///如果可以
- {
- for (j=1; j<=3; j++)
- for (l=1; l<=3; l++)
- a[j][l][tail]=a[j][l][head];
- a[nowx][nowy][tail]=a[tx][ty][head];
- a[tx][ty][tail]=0;///空位
- dx[tail]=tx;
- dy[tail]=ty;
- d[tail]=d[head]+1;///cc(tail);
- tail++;
- }
- }
- head++;
- }
- return 0;
- }
复制代码 |
|