当时手一残,有点小bug,被其他队的先a了。哎。
#include<iostream> #include<cstring> #include<stdio.h> #include<queue> using namespace std; int a[7],b[7]; bool visit[7][7][7][7][7][7]; int bfs() { queue<int> q[6]; queue<int> step; int c[6]; step.push(0); for(int i=0; i<6; i++) { q[i].push(b[i]); } visit[b[0]][b[1]][b[2]][b[3]][b[4]][b[5]]=true; while(!q[1].empty()) { for(int i=0; i<6; i++) { c[i]=q[i].front(); } int t=step.front(); bool flag=true; for(int i=0; i<6; i++) { if(c[i]!=a[i]) flag=false; } if(flag) return t; if(!visit[c[3]][c[2]][c[0]][c[1]][c[4]][c[5]]) //left { step.push(t+1); q[0].push(c[3]); q[1].push(c[2]); q[2].push(c[0]); q[3].push(c[1]); q[4].push(c[4]); q[5].push(c[5]); visit[c[3]][c[2]][c[0]][c[1]][c[4]][c[5]]=true; } // cout<<step.front()<<endl; if(!visit[c[2]][c[3]][c[1]][c[0]][c[4]][c[5]]) //right { step.push(t+1); q[0].push(c[2]); q[1].push(c[3]); q[2].push(c[1]); q[3].push(c[0]); q[4].push(c[4]); q[5].push(c[5]); visit[c[2]][c[3]][c[1]][c[0]][c[4]][c[5]]=true; } //cout<<"2"<<endl; if(!visit[c[5]][c[4]][c[2]][c[3]][c[0]][c[1]]) //back { step.push(t+1); q[0].push(c[5]); q[1].push(c[4]); q[2].push(c[2]); q[3].push(c[3]); q[4].push(c[0]); q[5].push(c[1]); visit[c[5]][c[4]][c[2]][c[3]][c[0]][c[1]]=true; } //cout<<"3"<<endl; if(!visit[c[4]][c[5]][c[2]][c[3]][c[1]][c[0]]) //front { step.push(t+1); q[0].push(c[4]); q[1].push(c[5]); q[2].push(c[2]); q[3].push(c[3]); q[4].push(c[1]); q[5].push(c[0]); visit[c[4]][c[5]][c[2]][c[3]][c[1]][c[0]]=true; } // cout<<"4"<<endl; for(int i=0; i<6; i++) { q[i].pop(); } step.pop(); } return -1; } int main() { while(~scanf("%d%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5])) { memset(visit,false,sizeof(visit)); for(int i=0; i<6; i++) scanf("%d",&b[i]); int count=0; for(int i=0; i<3; i++)// 剪枝 { int a1=a[i*2],a2=a[i*2+1]; for(int j=0; j<3; j++) { int b1=b[j*2],b2=b[j*2+1]; if( (a1==b1&&a2==b2) || (a1==b2)&& (a2==b1) ) count++; } } // cout<<count<<endl; if(count!=3) { cout<<"-1"<<endl; continue; } cout<<bfs()<<endl; } return 0; }