I - Dice

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. 
InputThere 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. OutputFor 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<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int aa[8],bb[8];
int book[7][7][7];
int flag;
struct note
{
    int a,b,c,d,e,f,s;
};
void bfs()
{
    queue<note>Q;
    note now,tmp;
    now.a=bb[0];
    now.b=bb[1];
    now.c=bb[2];
    now.d=bb[3];
    now.e=bb[4];
    now.f=bb[5];
    now.s=0;
    book[bb[0]][bb[2]][bb[4]]=1;
    Q.push(now);
    while(!Q.empty())
    {

        now=Q.front();
        Q.pop();
        if(now.a==aa[0]&&now.b==aa[1]&&now.c==aa[2]&&now.d==aa[3]&&now.e==aa[4]&&now.f==aa[5])
        {
            flag=1;
            printf("%d\n",now.s);
            break;
        }
        for(int i=0; i<4; i++)
        {
            if(i==0)
            {
                tmp.a=now.d;
                tmp.b=now.c;
                tmp.c=now.a;
                tmp.d=now.b;
                tmp.e=now.e;
                tmp.f=now.f;
            }
            if(i==1)
            {
                tmp.a=now.c;
                tmp.b=now.d;
                tmp.c=now.b;
                tmp.d=now.a;
                tmp.e=now.e;
                tmp.f=now.f;
            }
            if(i==2)
            {
                tmp.a=now.f;
                tmp.b=now.e;
                tmp.c=now.c;
                tmp.d=now.d;
                tmp.e=now.a;
                tmp.f=now.b;
            }
            if(i==3)
            {
                tmp.a=now.e;
                tmp.b=now.f;
                tmp.c=now.c;
                tmp.d=now.d;
                tmp.e=now.b;
                tmp.f=now.a;
            }
            if(book[tmp.a][tmp.c][tmp.e]==0)
            {

                book[tmp.a][tmp.c][tmp.e]=1;
                tmp.s=now.s+1;
                Q.push(tmp);
            }

        }
    }

}
int main()
{
    while(~scanf("%d",&aa[0]))
    {
        flag=0;
        int i,j;
        memset(book,0,sizeof(book));
        for(i=1; i<6; i++)
            scanf("%d",&aa[i]);
        for(j=0; j<6; j++)
            scanf("%d",&bb[j]);

        bfs();
        if(flag==0)printf("-1\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/guoshuyan12/article/details/75137102