hdu 5012__Dice

Dice

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


Problem Description
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.
 

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
 

Source
 
题意:给出一个骰子的起始态跟终态,问初始态能够通过几次的上下左右翻转变换得到终态。得不出就输出0.

想法:可以手写推出当起始态为123456的时候,向上下左右翻转的结果分别为:
上:653412
下:563421
左:431256
右:342156




这样就确定了bfs递推的方向。这里我是使用STL里的string类搞的。

代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<string>
using namespace std;

string fun(int n,string a)
{
    int t;
    string b=a;
    if(n==1) {
        b[0]=a[5];
        b[1]=a[4];
        b[2]=a[2];
        b[3]=a[3];
        b[4]=a[0];
        b[5]=a[1];
    }
    else if(n==2) {
        b[0]=a[4];
        b[1]=a[5];
        b[2]=a[2];
        b[3]=a[3];
        b[4]=a[1];
        b[5]=a[0];
    }
    else if(n==3) {
        b[0]=a[3];
        b[1]=a[2];
        b[2]=a[0];
        b[3]=a[1];
        b[4]=a[4];
        b[5]=a[5];
    }
    else if(n==4) {
        b[0]=a[2];
        b[1]=a[3];
        b[2]=a[1];
        b[3]=a[0];
        b[4]=a[4];
        b[5]=a[5];
    }
    return b;
}
int main()
{
    string ch,ch1;
    string::iterator it;
    queue<string> q;
    map<string ,int> vis,path;  //path记录走到这步需要多少步,vis记录这种状态是否已出现过
    string t,a;
    int i;
    char aa[10],bb[10];
    while(cin>>aa[0]) {     //输入这里开始使用的是scanf,发现即使使用%*c这样的格式控制符还是会wa,所以这里数据应该会有结尾有空格之类的情况。
        for(i=1;i<6;i++) {
            cin>>aa[i];
        }
        for(i=0;i<6;i++) {
            cin>>bb[i];
        }
        aa[6]='\0';bb[6]='\0';
        ch=aa;ch1=bb;
        if(ch==ch1) {   //特判两种状态开始就是一样
            printf("0\n");
            continue;
        }
        while(!q.empty()) {
            q.pop();
        }
        q.push(ch);
        vis.clear();
        path[ch]=0;     
        int ans=-1;
        while(!q.empty()) {
            t=q.front();
            vis[t]=1;
            q.pop();
            for(i=1;i<=4;i++) {
                a=fun(i,t);
                if(a==ch1) {
                    ans=path[t]+1;
                    goto fff;       //使用goto的做法真是极丑的QAQ
                }
                if(vis[a]==0) {
                    path[a]=path[t]+1;
                    q.push(a);
                    vis[t]=1;
                }
            }
        }
        fff:
        if(ans!=-1)
            printf("%d\n",ans);
        else {
            printf("-1\n");
        }
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值