Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1931 Accepted Submission(s): 960
Problem Description
There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a
1.a
2,a
3,a
4,a
5,a
6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b
1.b
2,b
3,b
4,b
5,b
6 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 a
i ≠ a
j and b
i ≠ b
j 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, a i ≠ b i). Ddy wants to make the two dices look the same from all directions(which means for all i, a i = b i) 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.
At the beginning, the two dices may face different(which means there exist some i, a i ≠ b i). Ddy wants to make the two dices look the same from all directions(which means for all i, a i = b i) 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 a 1,a 2,a 3,a 4,a 5,a 6, representing the numbers on dice A.
The second line consists of six integers b 1,b 2,b 3,b 4,b 5,b 6, representing the numbers on dice B.
For each case, the first line consists of six integers a 1,a 2,a 3,a 4,a 5,a 6, representing the numbers on dice A.
The second line consists of six integers b 1,b 2,b 3,b 4,b 5,b 6, 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
Source
只有四个方向,技巧:开一个六维的数组,对骰子的状态进行标记,如果已经进入队列了就不再push了;
一直等到队列为空还没有找到与之相配的状态则ans=-1;
代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int v[7][7][7][7][7][7];
struct info
{
int a1,a2,a3,a4,a5,a6,num;
} x,y;
int ans=0;
queue<info>que;
void bfs()
{
while(!que.empty()) que.pop();
if(y.a1==x.a1&&y.a2==x.a2&&y.a3==x.a3&&y.a4==x.a4&&y.a5==x.a5&&y.a6==x.a6)
return ;
x.num=0;
que.push(x);
info B;
while(!que.empty())
{
info A=que.front();
que.pop();
B.num=A.num+1;
//左转
B.a1=A.a4,B.a2=A.a3,B.a3=A.a1,B.a4=A.a2,B.a5=A.a5,B.a6=A.a6;
if(B.a1==y.a1&&B.a2==y.a2&&B.a3==y.a3&&B.a4==y.a4&&B.a5==y.a5&&B.a6==y.a6)
{
ans=B.num;
return ;
}
else
{
if(!v[B.a1][B.a2][B.a3][B.a4][B.a5][B.a6])
{
que.push(B);
v[B.a1][B.a2][B.a3][B.a4][B.a5][B.a6]=1;
}
}
//右转
B.a1=A.a3,B.a2=A.a4,B.a3=A.a2,B.a4=A.a1,B.a5=A.a5,B.a6=A.a6;
if(B.a1==y.a1&&B.a2==y.a2&&B.a3==y.a3&&B.a4==y.a4&&B.a5==y.a5&&B.a6==y.a6)
{
ans=B.num;
return ;
}
else
{
if(!v[B.a1][B.a2][B.a3][B.a4][B.a5][B.a6])
{
que.push(B);
v[B.a1][B.a2][B.a3][B.a4][B.a5][B.a6]=1;
}
}
//上翻
B.a1=A.a5,B.a2=A.a6,B.a3=A.a3,B.a4=A.a4,B.a5=A.a2,B.a6=A.a1;
if(B.a1==y.a1&&B.a2==y.a2&&B.a3==y.a3&&B.a4==y.a4&&B.a5==y.a5&&B.a6==y.a6)
{
ans=B.num;
return ;
}
else
{
if(!v[B.a1][B.a2][B.a3][B.a4][B.a5][B.a6])
{
que.push(B);
v[B.a1][B.a2][B.a3][B.a4][B.a5][B.a6]=1;
}
}
//下翻
B.a1=A.a6,B.a2=A.a5,B.a3=A.a3,B.a4=A.a4,B.a5=A.a1,B.a6=A.a2;
if(B.a1==y.a1&&B.a2==y.a2&&B.a3==y.a3&&B.a4==y.a4&&B.a5==y.a5&&B.a6==y.a6)
{
ans=B.num;
return ;
}
else
{
if(!v[B.a1][B.a2][B.a3][B.a4][B.a5][B.a6])
{
que.push(B);
v[B.a1][B.a2][B.a3][B.a4][B.a5][B.a6]=1;
}
}
}
ans=-1;
return ;
}
int main()
{
while(~scanf("%d%d%d%d%d%d",&x.a1,&x.a2,&x.a3,&x.a4,&x.a5,&x.a6))
{
scanf("%d%d%d%d%d%d",&y.a1,&y.a2,&y.a3,&y.a4,&y.a5,&y.a6);
memset(v,0,sizeof(v));
ans=0;
bfs();
printf("%d\n",ans);
}
return 0;
}