Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 98 Accepted Submission(s): 57
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
Recommend
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define N 700005
struct stud{
int a1,a2,a3,a4,a5,a0;
int time;
}s,e,t;
int ans;
int vis[N];
int fdd(struct stud x)
{
int temp=0;
temp+=x.a0*1;
temp+=x.a1*7;
temp+=x.a2*7*7;
temp+=x.a3*7*7*7;
temp+=x.a4*7*7*7*7;
temp+=x.a5*7*7*7*7*7;
return temp;
}
stud fddd(stud cur,int i)
{
struct stud next;
if(i==0)
{
next.a0=cur.a2;
next.a1=cur.a3;
next.a2=cur.a1;
next.a3=cur.a0;
next.a4=cur.a4;
next.a5=cur.a5;
}
else
if(i==1)
{
next.a0=cur.a3;
next.a1=cur.a2;
next.a2=cur.a0;
next.a3=cur.a1;
next.a4=cur.a4;
next.a5=cur.a5;
}
else
if(i==2)
{
next.a0=cur.a5;
next.a1=cur.a4;
next.a2=cur.a2;
next.a3=cur.a3;
next.a4=cur.a0;
next.a5=cur.a1;
}
else
{
next.a0=cur.a4;
next.a1=cur.a5;
next.a2=cur.a2;
next.a3=cur.a3;
next.a4=cur.a1;
next.a5=cur.a0;
}
return next;
}
int bfs()
{
int temp,i;
struct stud cur,next;
memset(vis,0,sizeof(vis));
temp=fdd(s);
ans=fdd(e);
vis[temp]=1;
cur=s;
cur.time=0;
queue<stud>q;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
if(fdd(cur)==ans)
return cur.time;
for(i=0;i<4;i++) //旋转
{
stud t=fddd(cur,i);
temp=fdd(t);
if(!vis[temp])
{
t.time=cur.time+1;
q.push(t);
vis[temp]=1;
}
}
}
return -1;
}
int main()
{
int i;
while(~scanf("%d",&s.a0))
{
scanf("%d%d%d%d%d",&s.a1,&s.a2,&s.a3,&s.a4,&s.a5);
scanf("%d%d%d%d%d%d",&e.a0,&e.a1,&e.a2,&e.a3,&e.a4,&e.a5);
printf("%d\n",bfs());
}
}