/* ID:tianlin2 PROG:clocks LANG:C++ */ #include <iostream> #include <queue> #include <fstream> #include <cmath> #define MAX 262150 using namespace std; ifstream fin("clocks.in"); ofstream fout("clocks.out"); class node { public: int curs; int pres; int premove; node() { curs=pres=0; premove=0; } }nodes[MAX]; int v[MAX]={0}; int change[20]; int state=0; int smove[10][6]= { {0, 0, 0, 0, 0, 0}, {4, 1, 2, 4, 5, 0}, {3, 1, 2, 3, 0, 0}, {4, 2, 3, 5, 6, 0}, {3, 1, 4, 7, 0, 0}, {5, 2, 4, 5, 6, 8}, {3, 3, 6, 9, 0, 0}, {4, 4, 5, 7, 8, 0}, {3, 7, 8, 9, 0, 0}, {4, 5, 6, 8, 9, 0} }; void cstate(int &a,int which) { int bit1,bit2; if(a&change[(which-1)*2]) bit1=1; else bit1=0; if(a&change[(which-1)*2+1]) bit2=1; else bit2=0; if(bit1==0){ a=a+change[(which-1)*2]; } else{ if(bit2==0){ a=a-change[(which-1)*2]; a=a+change[(which-1)*2+1]; } else{ a=a-change[(which-1)*2]; a=a-change[(which-1)*2+1]; } } } /* void print(node* a) { if(a->curs==state) return; else{ print(&nodes[a->pres]); cout<<a->premove<<endl; } }*/ void print(int curState) { if(curState == state) return; else { print(nodes[curState].pres); if(curState==0) fout<<nodes[curState].premove; else fout<<nodes[curState].premove<<" "; } } void bfs() { queue<node*> bfsq; bfsq.push(&nodes[state]); v[state]=1; node* curnode; while(!bfsq.empty()){ curnode=bfsq.front(); bfsq.pop(); int curstate=curnode->curs; if(curstate==0){ print(curstate); fout<<endl; return; } for(int i=1;i<10;++i){ int newstate=curstate; for(int j=1;j<=smove[i][0];++j) cstate(newstate,smove[i][j]); if(!v[newstate]){ v[newstate]=1; nodes[newstate].pres=curstate; nodes[newstate].premove=i; bfsq.push(&nodes[newstate]); } } } } int main() { int i,temp; for(i=0;i!=9;++i){ fin>>temp; switch(temp){ case 3: temp=1; break; case 6: temp=2; break; case 9: temp=3; break; case 12: temp=0; break; } state=state+temp*pow(double(4),i); } change[0]=1; for(i=1;i!=19;++i) change[i]=change[i-1]*2; for(i=0;i!=MAX;++i) nodes[i].curs=i; bfs(); //system("pause"); return 0; }