标签:des style http color io os java ar strong
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 5 6 4 3 1 2 3 4 5 6 1 4 2 5 3 6
0 3 -1
#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; }
标签:des style http color io os java ar strong
原文地址:http://blog.csdn.net/u011721440/article/details/39345045