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
有效信息提取:
两个正方体;
1~6分别表示上、下、左、右、前、后;
四个操作:
左翻、右翻、前翻、后翻
目的:
对其中一个执行四个操作,输出最小步数让两个正方体完全相同(可能实现不了)
思考:
两个正方体等价;
最小步数→广搜;
两个正方体;
1~6分别表示上、下、左、右、前、后;
四个操作:
左翻、右翻、前翻、后翻
目的:
对其中一个执行四个操作,输出最小步数让两个正方体完全相同(可能实现不了)
思考:
两个正方体等价;
最小步数→广搜;
六个面用数组存储→四个操作转化为数值交换;
用队列存储变换后正方体;
如果没有出现过再加入队列,相同时跳出,队列为空时结束;用结构体存储,对应的操作次数存储在结构体中;
#include <iostream>
#include <cstring>
#include <queue>
#define N 1000000
using namespace std;
int a[6];
int b[6];
struct node{
int x[6];
int d;
};
int show[N];
int main()
{
while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5])
{
cin>>b[0]>>b[1]>>b[2]>>b[3]>>b[4]>>b[5];
memset(show,0,sizeof(show));
show[b[0]*100000+b[1]*10000+b[2]*1000+b[3]*100+b[4]*10+b[5]]=1;
queue<node> q;
node x,y;
for(int j=0;j<6;j++)
x.x[j]=b[j];
x.d=0;
int i;
q.push(x);
while(!q.empty())
{
x=q.front();
q.pop();
for(i=0;i<6;i++)
{
if(a[i]!=x.x[i])
break;
}
if(i==6)
break;
y.x[0]=x.x[3];
y.x[1]=x.x[2];
y.x[2]=x.x[0];
y.x[3]=x.x[1];
y.x[4]=x.x[4];
y.x[5]=x.x[5];
if(show[y.x[0]*100000+y.x[1]*10000+y.x[2]*1000
+y.x[3]*100+y.x[4]*10+y.x[5]]!=1)
{
y.d=x.d+1;
q.push(y);
show[y.x[0]*100000+y.x[1]*10000+y.x[2]*1000
+y.x[3]*100+y.x[4]*10+y.x[5]]=1;
}
y.x[0]=x.x[2];
y.x[1]=x.x[3];
y.x[2]=x.x[1];
y.x[3]=x.x[0];
y.x[4]=x.x[4];
y.x[5]=x.x[5];
if(show[y.x[0]*100000+y.x[1]*10000+y.x[2]*1000
+y.x[3]*100+y.x[4]*10+y.x[5]]!=1)
{
y.d=x.d+1;
q.push(y);
show[y.x[0]*100000+y.x[1]*10000+y.x[2]*1000
+y.x[3]*100+y.x[4]*10+y.x[5]]=1;
}
y.x[0]=x.x[5];
y.x[1]=x.x[4];
y.x[2]=x.x[2];
y.x[3]=x.x[3];
y.x[4]=x.x[0];
y.x[5]=x.x[1];
if(show[y.x[0]*100000+y.x[1]*10000+y.x[2]*1000
+y.x[3]*100+y.x[4]*10+y.x[5]]!=1)
{
y.d=x.d+1;
q.push(y);
show[y.x[0]*100000+y.x[1]*10000+y.x[2]*1000
+y.x[3]*100+y.x[4]*10+y.x[5]]=1;
}
y.x[0]=x.x[4];
y.x[1]=x.x[5];
y.x[2]=x.x[2];
y.x[3]=x.x[3];
y.x[4]=x.x[1];
y.x[5]=x.x[0];
if(show[y.x[0]*100000+y.x[1]*10000+y.x[2]*1000
+y.x[3]*100+y.x[4]*10+y.x[5]]!=1)
{
y.d=x.d+1;
q.push(y);
show[y.x[0]*100000+y.x[1]*10000+y.x[2]*1000
+y.x[3]*100+y.x[4]*10+y.x[5]]=1;
}
}
if(i==6)
cout<<x.d<<endl;
else
cout<<-1<<endl;
}
return 0;
}