- #include<iostream>
- #include<set>
- #include<queue>
- using namespace std;
- int n=9,t,l;
- string const goal="000000000";
- string s,p,qq,ans;
- const int v[10][5]={
- {0,0,0,0,0},
- {0,1,3,4,0},
- {0,1,2,0,0},
- {1,2,4,5,0},
- {0,3,6,0,0},
- {1,3,4,5,7},
- {2,5,8,0,0},
- {3,4,6,7,0},
- {6,7,8,0,0},
- {4,5,7,8,0},},
- ll[10]={0,4,3,4,3,5,3,4,3,4};
- set<string>se;
- struct ha
- {
- string clock,step;
- ha(string str,string st):clock(str),step(st) {}
- };
- queue<ha>q;
- string change(string ss,int k)
- {
- for(int i=0;i<ll[k];i++)ss[v[k][i]]=(ss[v[k][i]]-'0'+1)%4+'0';
- return ss;
- }
- int main()
- {
- for(int i=1;i<=n;++i)cin>>t,s+=t/3%4+'0';
- q.push(ha(s,"")),se.insert(s);
- while(!q.empty())
- {
- string a=q.front().clock,b=q.front().step;
- if(a==goal)break;
- else for(int i=1;i<=n;i++)
- {
- p=change(a,i),qq=b+char(i+'0');
- if(!se.count(p))se.insert(p),q.push(ha(p,qq));
- }
- q.pop();
- }
- ans=q.front().step,l=ans.size();
- for(int i=0;i<l;i++)cout<<ans[i]<<' ';
- return 0;
- }
复制代码 |