Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 577 Accepted Submission(s): 335
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)
![](https://i-blog.csdnimg.cn/blog_migrate/4d5d5c19f17f92722270306477e25374.jpeg)
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
用队列进行广搜就行了,开一个6维数组。
#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;
}