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, ai ≠ 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.
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
题意:两个筛子通过翻转变成六个面一样,最少翻转多少次。如果不能成功输出-1。
思路:前后不动进行左右翻转,左右不动进行上下翻转。利用广搜求最少次数。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<algorithm>
using namespace std;
struct lou
{
int a,b,c,d,e,f;
int time;
};
int a1,b1,c1,d1,e1,f1,a2,b2,c2,d2,e2,f2;
int book[10][10][10][10][10][10];
void bfs()
{
memset(book,0,sizeof(book));
queue<lou>Q;
lou p,q;
p.a=a1;
p.b=b1;
p.c=c1;
p.d=d1;
p.e=e1;
p.f=f1;
book[a1][b1][c1][d1][e1][f1]=1;
p.time=0;
Q.push(p);
while(!Q.empty())
{
p=Q.front();
Q.pop();
if(p.a==a2&&p.b==b2&&p.c==c2&&p.d==d2&&p.e==e2&&p.f==f2)
{
printf("%d\n",p.time);
return;
}
int i;
for(i=0; i<4; i++)
{
if(i==0)
{
q.a=p.d;
q.b=p.c;
q.c=p.a;
q.d=p.b;
q.e=p.e;
q.f=p.f;
}
if(i==1)
{
q.a=p.c;
q.b=p.d;
q.c=p.b;
q.d=p.a;
q.e=p.e;
q.f=p.f;
}
if(i==2)
{
q.a=p.e;
q.b=p.f;
q.c=p.c;
q.d=p.d;
q.e=p.b;
q.f=p.a;
}
if(i==3)
{
q.a=p.f;
q.b=p.e;
q.c=p.c;
q.d=p.d;
q.e=p.a;
q.f=p.b;
}
q.time=p.time+1;
if(book[q.a][q.b][q.c][q.d][q.e][q.f]==0)
{
book[q.a][q.b][q.c][q.d][q.e][q.f]=1;
Q.push(q);
}
}
}
printf("-1\n");
}
int main()
{
while(~scanf("%d%d%d%d%d%d",&a1,&b1,&c1,&d1,&e1,&f1))
{
scanf("%d%d%d%d%d%d",&a2,&b2,&c2,&d2,&e2,&f2);
bfs();
}
return 0;
}