标签:
http://acm.hdu.edu.cn/showproblem.php?pid=5012
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1449 Accepted Submission(s):
742
#include<cstdio> #include<string> #include<iostream> #include<algorithm> #include<cmath> #include<queue> #include<cstring> using namespace std; struct node{ int x1,x2,x3,x4,x5,x6; int step; }; int c[7][7][7][7][7][7]; int main() { int i,j,n,k,m; queue<node>q; node a,d,t,b; bool flag; while(~scanf("%d%d%d%d%d%d",&a.x1,&a.x2,&a.x3,&a.x4,&a.x5,&a.x6)){//初始状态 scanf("%d%d%d%d%d%d",&b.x1,&b.x2,&b.x3,&b.x4,&b.x5,&b.x6);//最终状态 memset(c,0,sizeof(c));//清空标记数组 while(!q.empty()){ q.pop(); }//清空队列 c[a.x1][a.x2][a.x3][a.x4][a.x5][a.x6]=1;//标记已搜索 a.step=0; q.push(a); flag=false; while(!q.empty()){ d=q.front(); q.pop(); if(d.x1==b.x1&&d.x2==b.x2&&d.x3==b.x3&&b.x4==d.x4&&d.x5==b.x5&&d.x6==b.x6){//得到最终状态 flag=true; break; } //向前:左右两面不变 t.x1=d.x6; t.x2=d.x5; t.x3=d.x3; t.x4=d.x4; t.x5=d.x1; t.x6=d.x2; t.step=d.step+1; if(c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]==0){//判断是否已搜索过 c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]=1;//标记已搜索 q.push(t); } //向后:左右两面不变 t.x1=d.x5; t.x2=d.x6; t.x3=d.x3; t.x4=d.x4; t.x5=d.x2; t.x6=d.x1; t.step=d.step+1; if(c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]==0){ c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]=1; q.push(t); } //向左:前后两面不变 t.x1=d.x4; t.x2=d.x3; t.x3=d.x1; t.x4=d.x2; t.x5=d.x5; t.x6=d.x6; t.step=d.step+1; if(c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]==0){ c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]=1; q.push(t); } //向右:前后两面不变 t.x1=d.x3; t.x2=d.x4; t.x3=d.x2; t.x4=d.x1; t.x5=d.x5; t.x6=d.x6; t.step=d.step+1; if(c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]==0){ c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]=1; q.push(t); } } if(flag==false){ printf("-1\n"); } else printf("%d\n",d.step); } return 0; }
标签:
原文地址:http://www.cnblogs.com/acm31415/p/4795732.html