Total Submission(s): 577 Accepted Submission(s): 335
At the beginning, the two dices may face different(which means there exist some i, ai ≠ bi). Ddy wants to make the two dices look the same from all directions(which means for all i, ai = bi) only by the following four rotation operations.(Please read the picture for more information)
Now Ddy wants to calculate the minimal steps that he has to take to achieve his goal.
For each case, the first line consists of six integers a1,a2,a3,a4,a5,a6, representing the numbers on dice A.
The second line consists of six integers b1,b2,b3,b4,b5,b6, representing the numbers on dice B.
#include"stdio.h" #include"string.h" #include"queue" #include"iostream" #include"algorithm" using namespace std; #define N 7 #define LL __int64 int p[N][N][N][N][N][N]; int a[N],b[N]; struct node { int c[N],t; friend bool operator<(node a,node b) { return a.t>b.t; } }; int bfs() { int i,flag=-1; memset(p,0,sizeof(p)); priority_queue<node>q; node cur,next; for(i=1;i<N;i++) cur.c[i]=a[i]; cur.t=0; q.push(cur); p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=1; while(!q.empty()) { cur=q.top(); q.pop(); for(i=1;i<N;i++) if(b[i]!=cur.c[i]) break; if(i==N) { flag=cur.t; break; } next.t=cur.t+1; next.c[1]=cur.c[4]; next.c[2]=cur.c[3]; next.c[3]=cur.c[1]; next.c[4]=cur.c[2]; next.c[5]=cur.c[5]; next.c[6]=cur.c[6]; for(i=1;i<N;i++) a[i]=next.c[i]; if(p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]==0) { q.push(next); p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=1; } next.c[1]=cur.c[3]; next.c[2]=cur.c[4]; next.c[3]=cur.c[2]; next.c[4]=cur.c[1]; for(i=1;i<N;i++) a[i]=next.c[i]; if(p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]==0) { q.push(next); p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=1; } next.c[1]=cur.c[6]; next.c[2]=cur.c[5]; next.c[3]=cur.c[3]; next.c[4]=cur.c[4]; next.c[5]=cur.c[1]; next.c[6]=cur.c[2]; for(i=1;i<N;i++) a[i]=next.c[i]; if(p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]==0) { q.push(next); p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=1; } next.c[1]=cur.c[5]; next.c[2]=cur.c[6]; next.c[5]=cur.c[2]; next.c[6]=cur.c[1]; for(i=1;i<N;i++) a[i]=next.c[i]; if(p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]==0) { q.push(next); p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=1; } } return flag; } int main() { int i; while(scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])!=-1) { for(i=1;i<N;i++) scanf("%d",&b[i]); int ans=bfs(); printf("%d\n",ans); } return 0; }