Dice HDU - 5012

问题:

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

大意:

给你两个筛子六面的数,顺序为:上 下 左 右 前 后。问你第一个筛子能不能通过图示四中旋转方法:左右下上前后,右左上下前后,前后左右下上,后前左右上下(旋转后的状态),这四中方法变成第二个筛子所对应的顺序。

思路:

是一个广搜题,重复状态如何判断很重要,这里我用了set容器去标记状态和判断重复状态,方便快捷。其次就是状态的存储,我在结构体体面把数字转换成了字符串去存储状态。然后暴力。

代码:

#define N 7
#include<set>//利用了这个容器不能存储相同键值和可以快速查找键值的特点
#include<queue>
#include<string>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int d;
char k[N],s[N];
set<string>se;//用于标记筛子状态
struct node
{
    int step;
    char st[N];//存储当前筛子状态
};
void bfs()
{
    queue<node>q;
    node h,t;
    h.step=0;
    strcpy(h.st,k);
    se.insert(h.st);//入容器
    q.push(h);
    while(!q.empty())
    {
        h=q.front();
        q.pop();
        for(int i=1; i<=4; i++)//暴力每一种状态
        {
            t=h;char a,b,c,d;
            //改变数组中数的位置,相当于旋转筛子
            if(i==1)
            {
                a=t.st[0];b=t.st[1];c=t.st[2];d=t.st[3];
                t.st[0]=c;t.st[1]=d;t.st[2]=b;t.st[3]=a;
            }
            else if(i==2)
            {
                a=t.st[0];b=t.st[1];c=t.st[2];d=t.st[3];
                t.st[0]=d;t.st[1]=c;t.st[2]=a;t.st[3]=b;
            }
            else if(i==3)
            {
                a=t.st[0];b=t.st[1];c=t.st[4];d=t.st[5];
                t.st[0]=c;t.st[1]=d;t.st[4]=b;t.st[5]=a;
            }
            else if(i==4)
            {
                a=t.st[0];b=t.st[1];c=t.st[4];d=t.st[5];
                t.st[0]=d;t.st[1]=c;t.st[4]=a;t.st[5]=b;
            }
            if(se.find(t.st)!=se.end())continue;//检测这种情况是不是已经存在过
            se.insert(t.st);//把这种情况记录下来
            t.step=h.step+1;
            q.push(t);
            if(strcmp(s,t.st)==0)//可以旋转到
            {
                printf("%d\n",t.step);
                return;
            }
        }
    }
    printf("-1\n");//不能旋转到
    return;
}
int main()
{
    while(~scanf("%d",&d))
    {
        se.clear();
        k[0]=d+'0';
        for(int i=1; i<6; i++)
        {
            scanf("%d",&d);
            k[i]=d+'0';
        }
        for(int i=0; i<6; i++)
        {
            scanf("%d",&d);
            s[i]=d+'0';
        }
        if(strcmp(k,s)==0)//如果两筛子状态一样,直接输出
            printf("0\n");
        else
            bfs();
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值