hdu 5012 Dice(bfs)

Dice

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


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

题意:

给两个骰子,问一个骰子通过几步变成另一个骰子的状态。

思路:

用BFS来搜索状态,最多不会超过6次。超过6次就不再往下搜寻。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=10000;
struct node
{
    int up,down,left,right,front,back;
    int cnt;
    friend bool operator == (node a,node b)
    {
        return a.up==b.up&&a.down==b.down&&a.left==b.left&&a.right==b.right&&a.front==b.front&&a.back==b.back;
    }
};
queue<node> que;
node lrotation(node a)
{
    node p;
    int tmp;
    tmp=a.down;
    a.down=a.left;
    a.left=a.up;
    a.up=a.right;
    a.right=tmp;
    p=a;
    p.cnt=a.cnt+1;
    return p;
}
node rrotation(node a)
{
    node p;
    int tmp;
    tmp=a.down;
    a.down=a.right;
    a.right=a.up;
    a.up=a.left;
    a.left=tmp;
    p=a;
    p.cnt=a.cnt+1;
    return p;
}
node frotation(node a)
{
    node p;
    int tmp;
    tmp=a.front;
    a.front=a.up;
    a.up=a.back;
    a.back=a.down;
    a.down=tmp;
    p=a;
    p.cnt=a.cnt+1;
    return p;
}
node brotation(node a)
{
    node p;
    int tmp;
    tmp=a.front;
    a.front=a.down;
    a.down=a.back;
    a.back=a.up;
    a.up=tmp;
    p=a;
    p.cnt=a.cnt+1;
    return p;
}
int main()
{
    node p;
    int ans=0;
    while(~scanf("%d%d%d%d%d%d",&p.up,&p.down,&p.left,&p.right,&p.front,&p.back))
    {
        p.cnt=0;
        while(!que.empty()) que.pop();
        node a;
        a.cnt=0;
        scanf("%d%d%d%d%d%d",&a.up,&a.down,&a.left,&a.right,&a.front,&a.back);
        if(a==p)
            ans=0;
        else
        {
            que.push(a);
            ans=-1;
            while(!que.empty())
            {
                node tmp=que.front();
                que.pop();
                node now;
                now=lrotation(tmp);
                if(now==p)
                {
                    ans=now.cnt;
                    break;
                }
                else if(now.cnt<=6)
                    que.push(now);
                now=rrotation(tmp);
                if(now==p)
                {
                    ans=now.cnt;
                    break;
                }
                else if(now.cnt<=6)
                    que.push(now);
                now=frotation(tmp);
                if(now==p)
                {
                    ans=now.cnt;
                    break;
                }
                else if(now.cnt<=6)
                    que.push(now);
                now=brotation(tmp);
                if(now==p)
                {
                    ans=now.cnt;
                    break;
                }
                else if(now.cnt<=6)
                    que.push(now);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/timeclimber/article/details/80237560