Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 174 Accepted Submission(s): 100
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
给出两个骰子a b六个面的值 问 a能否通过四种操作 变成b所呈现的样子
四种操作就是向前后左右翻 然后排除重复的情况 最多就是64种情况 所以可以直接模拟出所有的情况
WA了几发 主要是在除重时的方法不对 蓝胖说了一种方法蛮好的 把每面的数字作为一个六位数其中的一位 换算成整数 用一个标记数组来存储是否出现过这个数 从而排除重复出现的情况
用优先队列来存储 总是选取当前步数最少的点来进行遍历
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <queue>
#define eps 1e-8
#define op operator
#define MOD 10009
#define MAXN 100100
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FOV(i,a,b) for(int i=a;i>=b;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define REV(i,a,b) for(int i=a-1;i>=b;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define ll __int64
using namespace std;
struct node
{
int steps;
int c[6];
int r,l,f,b;//四个方向
bool operator <(const node p)const
{
return steps>p.steps;
}
};
node no[700];
bool wocao[1000000];
int a[6];
int b[6];
int mi;
priority_queue<node> Q;
node p,q;
void bfs()
{
while(!Q.empty())
{
q=Q.top(); Q.pop();
int y=q.c[0]*100000+q.c[1]*10000+q.c[2]*1000+q.c[3]*100+q.c[4]*10+q.c[5];
if(wocao[y]) continue;
wocao[y]=1;
int flag=1;
for(int i=0;i<6;i++)
{
if(q.c[i]!=b[i])
{
flag=0; break;
}
}
if(flag)
{
mi=q.steps;
return;
}
p.steps=q.steps+1;
p.r=q.r+1;
p.c[0]=q.c[2];
p.c[1]=q.c[3];
p.c[2]=q.c[1];
p.c[3]=q.c[0];
p.c[4]=q.c[4];
p.c[5]=q.c[5];
Q.push(p);
p.steps=q.steps+1;
p.l=q.l+1;
p.c[0]=q.c[3];
p.c[1]=q.c[2];
p.c[2]=q.c[0];
p.c[3]=q.c[1];
p.c[4]=q.c[4];
p.c[5]=q.c[5];
Q.push(p);
p.steps=q.steps+1;
p.f=q.f+1;
p.c[0]=q.c[5];
p.c[1]=q.c[4];
p.c[2]=q.c[2];
p.c[3]=q.c[3];
p.c[4]=q.c[0];
p.c[5]=q.c[1];
Q.push(p);
p.steps=q.steps+1;
p.b=q.b+1;
p.c[0]=q.c[4];
p.c[1]=q.c[5];
p.c[2]=q.c[2];
p.c[3]=q.c[3];
p.c[4]=q.c[1];
p.c[5]=q.c[0];
Q.push(p);
}
return;
}
int main()
{
//freopen("ceshi.txt","r",stdin);
while(scanf("%d%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5])!=EOF)
{
for(int i=0;i<6;i++)
scanf("%d",&b[i]);
MEM(wocao,0);
q.steps=0;
for(int i=0;i<6;i++)
q.c[i]=a[i];
q.r=q.l=q.f=q.b=0;
Q.push(q);
mi=-1;
bfs();
while(!Q.empty()) Q.pop();
printf("%d\n",mi);
}
return 0;
}