H - 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, ai ≠ 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.

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

题意:

给你两个数组代表一个正方体上的几个面,让你通过几次翻转,把第二个正方体变成和第一个正方体一样,求出最小的翻转次数。

思路:

这道题是一道简单的广搜题,但是这道题难在怎样把翻转zhua转换成代码能表示出来,这是做题的关键。

代码如下:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

int a[7],b[7];
int book[7][7][7][7][7][7];//用来标记每次翻转后的情况,标记这种情况出现过;

struct node
{
    int x[7];//记录翻转后的情况;
    int step;
};

int bfs()
{
    int i;
    node aa,next;
    queue<node>Q;
    memset(book,0,sizeof book);
    for(i=0;i<6;i++)//把初始的情况入队列;
    {
        aa.x[i]=a[i];
    }
    aa.step=0;
    book[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]]=1;
    Q.push(aa);
    while(!Q.empty())
    {
        int i;
        aa=Q.front();
        Q.pop();
        if(aa.x[0]==b[0]&&aa.x[1]==b[1]&&aa.x[2]==b[2]&&aa.x[3]==b[3]&&aa.x[4]==b[4]&&aa.x[5]==b[5])
            return aa.step;//搜到了就还回最少的搜索步数;
        int q;
        for(i=0; i<4; i++)//把四种翻转情况都搜一遍;
        {
            next=aa;
            if(i==0)
            {
                q=next.x[0];
                next.x[0]=next.x[3];
                next.x[3]=next.x[1];
                next.x[1]=next.x[2];
                next.x[2]=q;
            }
            if(i==1)
            {
                q=next.x[0];
                next.x[0]=next.x[2];
                next.x[2]=next.x[1];
                next.x[1]=next.x[3];
                next.x[3]=q;
            }
            if(i==2)
            {
                q=next.x[0];
                next.x[0]=next.x[4];
                next.x[4]=next.x[1];
                next.x[1]=next.x[5];
                next.x[5]=q;
            }
            if(i==3)
            {
                q=next.x[0];
                next.x[0]=next.x[5];
                next.x[5]=next.x[1];
                next.x[1]=next.x[4];
                next.x[4]=q;
            }
            if(book[next.x[0]][next.x[1]][next.x[2]][next.x[3]][next.x[4]][next.x[5]]==0)//这种情况没有出现过;
            {
                next.step=aa.step+1;//步数加一;
                book[next.x[0]][next.x[1]][next.x[2]][next.x[3]][next.x[4]][next.x[5]]=1;//把这种情况标记为搜索过;
                Q.push(next);//入队列;
            }
        }
    }
    return -1;
}

int main()
{
    while(~scanf("%d",&a[0]))
    {
        int i;
        for(i=1; i<6; i++)
            scanf("%d",&a[i]);
        for(i=0; i<6; i++)
            scanf("%d",&b[i]);
        int kk=bfs();
        if(kk==-1)
            printf("-1\n");
        else
            printf("%d\n",kk);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/titi2018815/article/details/81162441