hdu 5012 Dice 【Dfs】

Dice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1702    Accepted Submission(s): 851

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

Source

2014 ACM/ICPC Asia Regional Xi'an Online

 

题目大意:

给你两个骰子,给出其上下左右前后的数字,问第一个最少几步能够转变成第二个骰子那样。

 

思路:


1、首先枚举出四种转变的方式:

①123456->563421

②123456->653412

③123456->342156

④123456->431256


2、其次想如何暴力搞掉这个题。最开始第一反应是最多走6步就能走到最终情况,否则输出-1,但是可惜,卡了TLE、


3、然后枚举了几种情况,发现其实四步以内就能从起始状态变成终止状态。大家不妨脑补一下。


4、然后就是Dfs四种情况咯!


Ac代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int ans;
int a[7];
int b[7];
int Dfs(int a[],int cur)
{
    if(cur>4)return 0;
    int flag=0;
    for(int i=1;i<=6;i++)
    {
        if(a[i]!=b[i])flag=1;
    }
    if(flag==1)
    {
        int tmp[7];
        tmp[1]=a[5];tmp[2]=a[6];tmp[3]=a[3];tmp[4]=a[4];tmp[5]=a[2];tmp[6]=a[1];
        Dfs(tmp,cur+1);
        tmp[1]=a[6];tmp[2]=a[5];tmp[3]=a[3];tmp[4]=a[4];tmp[5]=a[1];tmp[6]=a[2];
        Dfs(tmp,cur+1);
        tmp[1]=a[3];tmp[2]=a[4];tmp[3]=a[2];tmp[4]=a[1];tmp[5]=a[5];tmp[6]=a[6];
        Dfs(tmp,cur+1);
        tmp[1]=a[4];tmp[2]=a[3];tmp[3]=a[1];tmp[4]=a[2];tmp[5]=a[5];tmp[6]=a[6];
        Dfs(tmp,cur+1);
    }
    else
    {
        ans=min(ans,cur);
    }
}
int main()
{
    while(~scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]))
    {
        for(int i=1;i<=6;i++)
        {
            scanf("%d",&b[i],0);
        }
        ans=0x3f3f3f3f;
        Dfs(a,0);
        if(ans==0x3f3f3f3f)printf("-1\n");
        else
        printf("%d\n",ans);
    }
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值