HDU 5012 Dice(模拟+BFS)

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

解题思路

1.题意:输入第一行按顺序(上下左右前后)给出一个正方体六个面上的数字,问经过多次翻转后能否能翻转出第二行所示情况
2.翻转操作一共有四种,向左翻转(沿CG边),向右(沿DH边),向前(沿CD边),向后(沿GH边),模拟这四种翻转即可
3.关于翻转不到的情况有两种,第一种如样例3,上下面为1和2(对立面),而需要翻转出2和5是对立面,显然不可能。另一种情况类似于123456翻转出125634(如果12为上下面,则只通过左右前后翻转不可能翻转出12为上下面,而左右为56面的情况)显然是不可能的
4.如果感觉不好理解可以拿一个橡皮或者自己用纸撕出一个正方体,标上数字旋转一下即可
5.此类问题类似于倒水问题(见下篇博客《非常可乐》)只要处理好模拟中的细节问题就可以

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<memory.h>
#include<queue>
#include<string>
#include<set>
using namespace std;
typedef struct node {
    string s;//翻转出的每种情况
    int step;//记录步数
} Node;
Node temp,temps;
set<string>ss;
//set相当于标记数组,对于每种情况(字符串s)
//如果集合中存在则说明已经出现过,不需要再入队遍历
//否则没出现则压入集合
//这样既不用开6维的标记数组了
queue<Node>q;
int n[10];
string a="123456";
string b="123456";
int main() {
    while(scanf("%d",&n[0])!=EOF) {
    //输入整数转化成字符,这样不用吸收空格和换行
        for(int i=1; i<6; i++) {
            scanf("%d",&n[i]);
        }
        for(int i=0; i<6; i++) {
            a[i]=n[i]+'0';
            scanf("%d",&n[i]);
            b[i]=n[i]+'0';
        }
        //判断是否会出现对立面不一致的情况
        for(int i=0; i<6; i=i+2) {
            int j;
            for(j=0; j<6; j=j+2) {
                if((a[i]==b[j]&&a[i+1]==b[j+1])||(a[i]==b[j+1]&&a[i+1]==b[j])) {
                    break;
                }
            }
            if(j==6){
                flag++;
            }
        }
        int haha=-1;
        if(flag==3) {
            cout<<"-1"<<endl;
        }
        else {
            temp.s="123456";
            temp.s=a;
            temp.step=0;
            q.push(temp);
            ss.insert(a);//注意标记的使用方法
            while(!q.empty()) {
                temp=q.front();
                q.pop();
                if(temp.s==b) {
                    haha=temp.step;
                    break;
                }
                //向左
                a=temp.s;
                temps=temp;
                temp.s[4]=a[4];//每个面的变化
                temp.s[5]=a[5];
                temp.s[0]=a[3];
                temp.s[1]=a[2];
                temp.s[2]=a[0];
                temp.s[3]=a[1];
                temp.step=temps.step+1;
                if(ss.find(temp.s)==ss.end()) {
                    q.push(temp);
                    ss.insert(temp.s);
                }
                //向右
                temp.s[4]=a[4];
                temp.s[5]=a[5];
                temp.s[0]=a[2];
                temp.s[1]=a[3];
                temp.s[2]=a[1];
                temp.s[3]=a[0];
                temp.step=temps.step+1;
                if(ss.find(temp.s)==ss.end()) {
                    q.push(temp);
                    ss.insert(temp.s);
                }
                //向后
                temp.s[2]=a[2];
                temp.s[3]=a[3];
                temp.s[0]=a[4];
                temp.s[1]=a[5];
                temp.s[4]=a[1];
                temp.s[5]=a[0];
                temp.step=temps.step+1;
                if(ss.find(temp.s)==ss.end()) {
                    q.push(temp);
                    ss.insert(temp.s);
                }
                //向前
                temp.s[2]=a[2];
                temp.s[3]=a[3];
                temp.s[0]=a[5];
                temp.s[1]=a[4];
                temp.s[4]=a[0];
                temp.s[5]=a[1];
                temp.step=temps.step+1;
                if(ss.find(temp.s)==ss.end()) {
                    q.push(temp);
                    ss.insert(temp.s);
                }
            }
            while(!q.empty()){
                q.pop();
            }
            ss.clear();
            if(haha!=-1){
                cout<<haha<<endl;
            }
            else{
                cout<<"-1"<<endl;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值