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.
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,2)(3,4)(5,6),则四个旋转对应的结果分别为
Left Rotation:(4,3)(1,2)(5,6)
Right Rotation:(3,4)(2,1)(5,6)
Front Rotation:(6,5)(3,4)(1,2)
Back Rotation:(5,6)(3,4)(2,1)
然后把当前六个面状态压缩成int跑BFS就行了。还不懂的看代码就明白了。
代码:
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
bool vis[666666];
int A,B;
struct D{
int v,num;
D(){}
D(int _v,int _num):v(_v),num(_num){}
};
int BFS(){
memset(vis,false,sizeof vis);
queue<D> Q;
Q.push(D(A,0));
vis[A] = true;
D t;
int di[10];
while(!Q.empty()){
t = Q.front();
Q.pop();
if(t.v == B)return t.num;
int tt = t.v;
for(int i=6 ; i>=1 ; --i){
di[i] = tt%10;
tt /= 10;
}
int t1 = di[6] + di[5]*10 + di[1]*100 + di[2]*1000 + di[4]*10000 + di[3]*100000;
int t2 = di[6] + di[5]*10 + di[2]*100 + di[1]*1000 + di[3]*10000 + di[4]*100000;
int t3 = di[1] + di[2]*10 + di[4]*100 + di[3]*1000 + di[6]*10000 + di[5]*100000;
int t4 = di[2] + di[1]*10 + di[4]*100 + di[3]*1000 + di[5]*10000 + di[6]*100000;
if(!vis[t1]){
vis[t1] = true;
Q.push(D(t1,t.num+1));
}
if(!vis[t2]){
vis[t2] = true;
Q.push(D(t2,t.num+1));
}
if(!vis[t3]){
vis[t3] = true;
Q.push(D(t3,t.num+1));
}
if(!vis[t4]){
vis[t4] = true;
Q.push(D(t4,t.num+1));
}
}
return -1;
}
int main(){
int t;
while(scanf("%d",&A) != EOF){
for(int i=1 ; i<=5 ; ++i){
A *= 10;
scanf("%d",&t);
A += t;
}
B = 0;
for(int i=1 ; i<=6 ; ++i){
B *= 10;
scanf("%d",&t);
B += t;
}
printf("%d\n",BFS());
}
return 0;
}