Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 676 Accepted Submission(s): 385
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
题意:一次翻转有4种方式,问你将骰子的一种方式变成另一种方式至少需要多少步。
思路:广搜就可以了。
AC代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
int vis[150000],vc[810][1000][7];
int t,pow7[10],num[7],ans,op;
bool flag;
int suan(int pos)
{
int i,k=0;
for(i=1;i<=6;i++)
k+=num[i]*pow7[i];
if(k==op)
{
flag=true;
ans=pos+1;
}
if(vis[k]==t)
return 0;
else
{
vis[k]=t;
return 1;
}
}
void solve(int pos,int num2)
{
if(flag)
return;
int i,j,k;
//11111111111
num[1]=vc[pos][num2][3];
num[2]=vc[pos][num2][4];
num[3]=vc[pos][num2][2];
num[4]=vc[pos][num2][1];
num[5]=vc[pos][num2][5];
num[6]=vc[pos][num2][6];
k=suan(pos);
if(k==1)
{
vc[pos+1][0][0]++;
k=vc[pos+1][0][0];
for(i=1;i<=6;i++)
vc[pos+1][k][i]=num[i];
}
num[1]=vc[pos][num2][5];
num[2]=vc[pos][num2][6];
num[3]=vc[pos][num2][3];
num[4]=vc[pos][num2][4];
num[5]=vc[pos][num2][2];
num[6]=vc[pos][num2][1];
k=suan(pos);
if(k==1)
{
vc[pos+1][0][0]++;
k=vc[pos+1][0][0];
for(i=1;i<=6;i++)
vc[pos+1][k][i]=num[i];
}
num[1]=vc[pos][num2][6];
num[2]=vc[pos][num2][5];
num[3]=vc[pos][num2][3];
num[4]=vc[pos][num2][4];
num[5]=vc[pos][num2][1];
num[6]=vc[pos][num2][2];
k=suan(pos);
if(k==1)
{
vc[pos+1][0][0]++;
k=vc[pos+1][0][0];
for(i=1;i<=6;i++)
vc[pos+1][k][i]=num[i];
}
num[1]=vc[pos][num2][4];
num[2]=vc[pos][num2][3];
num[3]=vc[pos][num2][1];
num[4]=vc[pos][num2][2];
num[5]=vc[pos][num2][5];
num[6]=vc[pos][num2][6];
k=suan(pos);
if(k==1)
{
vc[pos+1][0][0]++;
k=vc[pos+1][0][0];
for(i=1;i<=6;i++)
vc[pos+1][k][i]=num[i];
}
}
int main()
{
int n,m,i,j,k;
pow7[1]=1;
for(i=2;i<=6;i++)
pow7[i]=pow7[i-1]*7;
while(~scanf("%d",&vc[0][1][1]))
{
t++;
for(i=2;i<=6;i++)
scanf("%d",&vc[0][1][i]);
vc[0][0][0]=1;
k=0;
for(i=1;i<=6;i++)
scanf("%d",&num[i]);
for(i=1;i<=6;i++)
k+=num[i]*pow7[i];
op=k;
k=0;
for(i=1;i<=6;i++)
k+=vc[0][1][i]*pow7[i];
if(k==op)
{
printf("0\n");
continue;
}
for(i=1;i<=800;i++)
vc[i][0][0]=0;
flag=false;
for(i=0;i<=800;i++)
{
//printf("solve %d\n",i);
if(flag ||vc[i][0][0]==0)
break;
for(j=1;j<=vc[i][0][0];j++)
{
if(flag)
break;
solve(i,j);
}
}
if(flag)
printf("%d\n",ans);
else
printf("-1\n");
}
}