hud 5012 Dice(隐式图的搜索 )

来源:互联网 发布:js怎么判断是不是数组 编辑:程序博客网 时间:2024/05/18 01:02

Problem Description
There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b1.b2,b3,b4,b5,b6 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 ai ≠ aj and bi ≠ bj 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, ai ≠ bi). Ddy wants to make the two dices look the same from all directions(which means for all i, ai = bi) 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 a1,a2,a3,a4,a5,a6, representing the numbers on dice A.

The second line consists of six integers b1,b2,b3,b4,b5,b6, 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

不喜欢这类题,感觉好麻烦啊,因为我不细心,出错了又要改好长时间
我的代码算是险过吧

#include<cstdio>#include<cstring>#include<stdlib.h>#include<fstream>#include<ctype.h>#include<math.h>#include<stack>#include<queue>#include<map>#include<set>#include<vector>#include<string>#include<iostream>#include<algorithm>#include<utility>#include<iomanip>#include<time.h>#include<iostream>#define lowbit(x) (x&-x)#define abs(x) ((x)>0?(x):-(x))using namespace std;typedef long long ll;const double Pi = acos(-1.0);const int N = 1e5+10, M = 1e3+20, mod = 1e9+7, inf = 2e9+10;const double e=2.718281828459 ;const double esp=1e-9;int num[7],num1[7],num2[7];int vis[654329];struct node{    int ss;    int step;};void Fun(int x){   for(int i = 5; i >=0; --i)    {        num[i] = x % 10;        x /= 10;    }}int get(){    int sum=0;    for(int i=0;i<6;i++)    {        sum=sum*10+num[i];    }    return sum;}int getfront(int x){    Fun(x);    int tmp = num[4];    num[4] = num[0];    num[0] = num[5];    num[5] = num[1];    num[1] = tmp;    return get();}int getleft(int x){    Fun(x);       int tmp = num[0];    num[0] = num[3];    num[3] = num[1];    num[1] = num[2];    num[2] = tmp;    return get();}int getright(int x){    Fun(x);      int tmp = num[0];    num[0] = num[2];    num[2] = num[1];    num[1] = num[3];    num[3] = tmp;    return get();}int getback(int x){    Fun(x);        int tmp = num[4];    num[4] = num[1];    num[1] = num[5];    num[5] = num[0];    num[0] = tmp;    return get();}void bfs(int star,int end){    queue<node>q;    node a;    a.ss=star;    a.step=0;    vis[a.ss]=1;    q.push(a);    while(!q.empty())    {        a=q.front();        q.pop(); node b;        if(a.ss==end) {   printf("%d\n",a.step);  return; }        int ff=a.ss,tem;        tem=getleft(ff);        if(!vis[tem])        {            b.step=a.step+1; vis[tem]=1; b.ss=tem; q.push(b);        }        tem=getright(ff);        if(!vis[tem])        {            b.step=a.step+1; vis[tem]=1; b.ss=tem; q.push(b);        }        tem=getfront(ff);         if(!vis[tem])        {            b.step=a.step+1; vis[tem]=1; b.ss=tem; q.push(b);        }        tem=getback(ff);        if(!vis[tem])        {            b.step=a.step+1; vis[tem]=1; b.ss=tem; q.push(b);        }    }    printf("-1\n");}int main(){//    std::ios::sync_with_stdio(false);//    std::cin.tie(0);    int star,end;    while(~scanf("%d",&num1[0]))    {        memset(vis,0,sizeof(vis));        star=0; end=0;        star=star*10+num1[0];        for(int i=1; i<6; i++)        {            scanf("%d",&num1[i]);            star=star*10+num1[i];        }        for(int i=0; i<6; i++)        {            scanf("%d",&num2[i]);            end=end*10+num2[i];        }        bfs(star,end);    }    return 0;}
0 0