首页 > 代码库 > hdu5012 Dice(bfs)

hdu5012 Dice(bfs)

题目链接:

点我点我

思路:主要是状态我没想到怎么表示,后来看的队友的,毕竟自己太弱。

题目:
 

Dice

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


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
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5017 5016 5015 5014 5013 
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;

const int maxn=999999+10;

struct cube
{
    int a1,a2,a3,a4,a5,a6;
    int key;
}st,en,next;

queue<cube>Q;
int ans;

bool vis[maxn];

int gao(cube c)
{
    int temp=0;
    temp+=c.a1*1;
    temp+=c.a2*10;
    temp+=c.a3*100;
    temp+=c.a4*1000;
    temp+=c.a5*10000;
    temp+=c.a6*100000;
    return temp;
}

cube solve(cube c,int k)
{
    cube temp;
    if(k==1)
    {
        temp.a1=c.a4;
        temp.a2=c.a3;
        temp.a3=c.a1;
        temp.a4=c.a2;
        temp.a5=c.a5;
        temp.a6=c.a6;
    }
    else if(k==2)
    {
        temp.a1=c.a3;
        temp.a2=c.a4;
        temp.a3=c.a2;
        temp.a4=c.a1;
        temp.a5=c.a5;
        temp.a6=c.a6;
    }
    else if(k==3)
    {
        temp.a1=c.a6;
        temp.a2=c.a5;
        temp.a3=c.a3;
        temp.a4=c.a4;
        temp.a5=c.a1;
        temp.a6=c.a2;
    }
    else
    {
        temp.a1=c.a5;
        temp.a2=c.a6;
        temp.a3=c.a3;
        temp.a4=c.a4;
        temp.a5=c.a2;
        temp.a6=c.a1;
    }
    return temp;
}

int bfs()
{
    ans=gao(en);
    memset(vis,false,sizeof(vis));
    while(!Q.empty())   Q.pop();
    st.key=0;
    Q.push(st);
    int temp=gao(st);
    vis[temp]=true;
    while(!Q.empty())
    {
        cube current=Q.front();
        Q.pop();
        if(ans==gao(current))
            return current.key;
        for(int i=1;i<=4;i++)
        {
           next=solve(current,i);
           next.key=current.key+1;
           int temp=gao(next);
           if(!vis[temp])
           {
               vis[temp]=true;
               Q.push(next);
           }
        }
    }
    return -1;
}


int main()
{
    while(~scanf("%d",&st.a1))
    {
        scanf("%d%d%d%d%d",&st.a2,&st.a3,&st.a4,&st.a5,&st.a6);
        scanf("%d%d%d%d%d%d",&en.a1,&en.a2,&en.a3,&en.a4,&en.a5,&en.a6);
        printf("%d\n",bfs());
    }
    return 0;
}



hdu5012 Dice(bfs)