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
题意:
第一个筛子通过几次翻转得到第二个筛子。
思路:
BFS ,利用筛子的顺序得到六位数用vis数组去重。
code:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;
const int d[4][6] = {{3, 2, 0, 1, 4, 5},
{2, 3, 1, 0, 4, 5},
{5, 4, 2, 3, 0, 1},
{4, 5, 2, 3, 1, 0}};
struct node
{
int a[10];
}c[30];
int ans;
int p[30], vis[666666];
int visit(node cc)
{
int s = 0;
for(int i = 0; i < 6; i ++){
s = s * 10 + cc.a[i];
}
return s;
}
int bfs()
{
int f = 0, r = 1;
memset(vis, 0, sizeof(vis));
p[0] = 0;
vis[visit(c[0])] = 1;
while(f < r){
if(visit(c[f]) == ans) return f;
for(int i = 0; i < 4; i ++){
for(int j = 0; j < 6; j ++){
c[r].a[j] = c[f].a[d[i][j]];
}
int ss = visit(c[r]);
if(!vis[ss]){
vis[ss] = 1;
p[r] = p[f] + 1;
r ++;
}
}
f ++;
}
return -1;
}
int main()
{
//freopen("in", "r", stdin);
while(~scanf("%d%d%d %d%d%d", &c[0].a[0],&c[0].a[1],&c[0].a[2],
&c[0].a[3],&c[0].a[4],&c[0].a[5])){
int n;
ans = 0;
for(int i=0;i<6;i++){
scanf("%d", &n);
ans = ans*10 +n;
}
int ff = bfs();
if(ff == -1) printf("-1\n");
else printf("%d\n", p[ff]);
}
return 0;
}