Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 243 Accepted Submission(s): 135
Problem Description
There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b1.b2,b3,b4,b5,b6 to be numbers on specific faces of dice B. It’s guaranteed that all numbers written on dices are integers no smaller than 1 and no more than 6 while ai ≠ aj and bi ≠ bj for all i ≠ j. Specially, sum of numbers on opposite faces may not be 7.
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.
Input
There are multiple test cases. Please process till EOF.
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.
Output
For each test case, print a line with a number representing the answer. If there’s no way to make two dices exactly the same, output -1.
Sample Input
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
Sample Output
0
3
-1
题目意思:
一个正方形,每个面有一个数字而且不相同(最多为6最小为1),正方形可以向左、右、前、后转,给出正方形两个状态,问最少转多少次可以从前面的状态转换成后面的状态,若不能输出-1。
思路:
正方形最多有6!种状态,由前面状态推到最后的状态,很明显bfs暴搜,和迷宫问题差不多。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 using namespace std; 7 8 struct node{ 9 int a[7], d; 10 }; 11 12 int visited[700000]; 13 14 int bfs(int x[],int y[]){ 15 queue<node>Q; 16 node p, q; 17 int num=0; 18 p.a[1]=x[1];p.a[2]=x[2];p.a[3]=x[3];p.a[4]=x[4];p.a[5]=x[5];p.a[6]=x[6];p.d=0; 19 visited[x[1]*100000+x[2]*10000+x[3]*1000+x[4]*100+x[5]*10+x[6]]=1; 20 Q.push(p); 21 while(!Q.empty()){ 22 p=Q.front(); 23 Q.pop(); 24 int f=1; 25 for(int i=1;i<=6;i++){ 26 if(p.a[i]!=y[i]){ 27 f=0;break; 28 } 29 } 30 if(f) return p.d; 31 //向左 32 q.a[1]=p.a[4];q.a[2]=p.a[3];q.a[3]=p.a[1];q.a[4]=p.a[2];q.a[5]=p.a[5];q.a[6]=p.a[6]; 33 if(!visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]){ 34 visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]=1; 35 q.d=p.d+1; 36 Q.push(q); 37 } 38 //向右 39 q.a[1]=p.a[3];q.a[2]=p.a[4];q.a[3]=p.a[2];q.a[4]=p.a[1];q.a[5]=p.a[5];q.a[6]=p.a[6]; 40 if(!visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]){ 41 visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]=1; 42 q.d=p.d+1; 43 Q.push(q); 44 } 45 //向前 46 q.a[1]=p.a[6];q.a[2]=p.a[5];q.a[3]=p.a[3];q.a[4]=p.a[4];q.a[5]=p.a[1];q.a[6]=p.a[2]; 47 if(!visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]){ 48 visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]=1; 49 q.d=p.d+1; 50 Q.push(q); 51 } 52 //向后 53 q.a[1]=p.a[5];q.a[2]=p.a[6];q.a[3]=p.a[3];q.a[4]=p.a[4];q.a[5]=p.a[2];q.a[6]=p.a[1]; 54 if(!visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]){ 55 visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]=1; 56 q.d=p.d+1; 57 Q.push(q); 58 } 59 } 60 return -1; 61 } 62 main() 63 { 64 int a[7], b[7]; 65 int i, j, k; 66 while(scanf("%d",&a[1])==1){ 67 memset(visited,0,sizeof(visited)); 68 for(i=2;i<=6;i++) scanf("%d",&a[i]); 69 for(i=1;i<=6;i++) scanf("%d",&b[i]); 70 printf("%d\n",bfs(a,b)); 71 } 72 }