题干:

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)

【HDU - 5012】Dice(模拟,bfs)_ide

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

题目大意:

给出两个正方体,每个正方体都有6种颜色,问是否可以经过左转,右转,前转,后转四种转法让两个正方体看起来一样(对应面颜色相同),最少转几次。

解题报告:

 直接bfs。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
vector<int> aa;
set<vector<int> > ss;
struct Node {
vector<int> vv;
int s;
};
int bfs(vector<int> st) {
queue<Node> q;
q.push({st,0});
while(q.size()) {
Node cur = q.front();q.pop();
if(cur.vv == aa) return cur.s;
if(ss.count(cur.vv)) continue;
ss.insert(cur.vv);
vector<int> t = cur.vv,nn(6);
//Left
nn[0] = t[3],nn[1] = t[2],nn[2] = t[0],nn[3] = t[1],nn[4] = t[4],nn[5] = t[5];
q.push({nn,cur.s+1});
//Right
nn[0] = t[2],nn[1] = t[3],nn[2] = t[1],nn[3] = t[0],nn[4] = t[4],nn[5] = t[5];
q.push({nn,cur.s+1});
//Front
nn[0] = t[5],nn[1] = t[4],nn[2] = t[2],nn[3] = t[3],nn[4] = t[0],nn[5] = t[1];
q.push({nn,cur.s+1});
//Back
nn[0] = t[4],nn[1] = t[5],nn[2] = t[2],nn[3] = t[3],nn[4] = t[1],nn[5] = t[0];
q.push({nn,cur.s+1});
}
return -1;
}
int main()
{
int z,x,c,v,b,n;
while(~scanf("%d%d%d%d%d%d",&z,&x,&c,&v,&b,&n)) {
aa.clear();
ss.clear();
vector<int> a(6);
a[0]=z;a[1]=x;a[2]=c;a[3]=v;a[4]=b;a[5]=n;
for(int i = 1; i<=6; i++) {
scanf("%d",&x);
aa.pb(x);
}
int ans = bfs(a);
printf("%d\n",ans);
}
return 0 ;
}