码迷,mamicode.com
首页 > 其他好文 > 详细

hdu5012 Dice

时间:2015-09-09 21:02:40      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

http://acm.hdu.edu.cn/showproblem.php?pid=5012

 

Dice

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


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
 

 

Recommend
hujie | We have carefully selected several similar problems for you: 5431 5430 5429 5428 5427
 
 
 
  这道题是个简单的搜索题,题意很简单,但是解的过程需要细心。
 题意:
  这道题输入给出两个正方体的状态:A,B正方体,每个正方体都有六个数字分别代表这个正方体的上、下、左、右、前、后六个面上的数字。对A正方体有四种操作:分别是向左翻滚,向右翻滚,向前翻滚,向后翻滚。如果对A正方体进行这四种操作,问能否得到B正方体的状态,如果可以,求需要进行多少次翻滚?如果不能则输出-1。
  这道题很明显可以用广搜来写,只要注意好每组样例结束后标记数组清空,队列清空,以及每次翻滚前后的状态关系就能AC了。
  
    以下是AC代码:
技术分享    
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
struct node{
    int x1,x2,x3,x4,x5,x6;
    int step;
};
int c[7][7][7][7][7][7];
int main()
{
    int i,j,n,k,m;
    queue<node>q;
    node a,d,t,b;
    bool flag;
    while(~scanf("%d%d%d%d%d%d",&a.x1,&a.x2,&a.x3,&a.x4,&a.x5,&a.x6)){//初始状态
        scanf("%d%d%d%d%d%d",&b.x1,&b.x2,&b.x3,&b.x4,&b.x5,&b.x6);//最终状态
        memset(c,0,sizeof(c));//清空标记数组
        while(!q.empty()){
            q.pop();
        }//清空队列
        c[a.x1][a.x2][a.x3][a.x4][a.x5][a.x6]=1;//标记已搜索
        a.step=0;
        q.push(a);
        flag=false;
        while(!q.empty()){
            d=q.front();
            q.pop();
            if(d.x1==b.x1&&d.x2==b.x2&&d.x3==b.x3&&b.x4==d.x4&&d.x5==b.x5&&d.x6==b.x6){//得到最终状态
                flag=true;
                break;
            }
            //向前:左右两面不变
            t.x1=d.x6;
            t.x2=d.x5;
            t.x3=d.x3;
            t.x4=d.x4;
            t.x5=d.x1;
            t.x6=d.x2;
            t.step=d.step+1;
            if(c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]==0){//判断是否已搜索过
                c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]=1;//标记已搜索
                q.push(t);
            }
            //向后:左右两面不变
            t.x1=d.x5;
            t.x2=d.x6;
            t.x3=d.x3;
            t.x4=d.x4;
            t.x5=d.x2;
            t.x6=d.x1;
            t.step=d.step+1;
            if(c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]==0){
                c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]=1;
                q.push(t);
            }
            //向左:前后两面不变
            t.x1=d.x4;
            t.x2=d.x3;
            t.x3=d.x1;
            t.x4=d.x2;
            t.x5=d.x5;
            t.x6=d.x6;
            t.step=d.step+1;
            if(c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]==0){
                c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]=1;
                q.push(t);
            }
            //向右:前后两面不变
            t.x1=d.x3;
            t.x2=d.x4;
            t.x3=d.x2;
            t.x4=d.x1;
            t.x5=d.x5;
            t.x6=d.x6;
            t.step=d.step+1;
            if(c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]==0){
                c[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]=1;
                q.push(t);
            }
        }
        if(flag==false){
            printf("-1\n");
        }
        else
            printf("%d\n",d.step);
    }
    return 0;
}

 

hdu5012 Dice

标签:

原文地址:http://www.cnblogs.com/acm31415/p/4795732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!