HDU5012 Dice

Dice


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


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

题意:两个色子,给出两个色子的初始状态(上下左右前后),问前面一个能不能通过转动变成后面一个。

写出四种转法转动后的状态,然后直接搜索。。。。记忆化搜索和BFS都行。。。好像我写的记忆化比BFS快....

记忆化搜索

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=1<<30;
int dp[7][7][7][7][7][7];
int su,sd,sl,sr,sf,sre;
int fu,fd,fl,fr,ff,fre;
int flag;
int dfs(int up,int down,int left,int right,int front,int rear)
{
    if(dp[up][down][left][right][front][rear]!=-1)
        return dp[up][down][left][right][front][rear];
    if(up==fu&&down==fd&&left==fl&&right==fr&&front==ff&&rear==fre)
    {
        flag=1;
        return dp[up][down][left][right][front][rear]=0;
    }
    dp[up][down][left][right][front][rear]=INF;
    int temp=INF;
    temp=min(temp,dfs(rear,front,left,right,up,down));
    temp=min(temp,dfs(front,rear,left,right,down,up));
    temp=min(temp,dfs(right,left,up,down,front,rear));
    temp=min(temp,dfs(left,right,down,up,front,rear));
    return dp[up][down][left][right][front][rear]=temp+1;
}
int main()
{
    while(scanf("%d%d%d%d%d%d",&su,&sd,&sl,&sr,&sf,&sre)==6)
    {
        scanf("%d%d%d%d%d%d",&fu,&fd,&fl,&fr,&ff,&fre);
        memset(dp,-1,sizeof(dp));
        flag=0;
        int ans=dfs(su,sd,sl,sr,sf,sre);
        if(flag)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}
BFS

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct Node
{
    int a[6],d;
    Node()
    {
        memset(a,0,sizeof(a));
        d=0;
    }
}s,e;
int hash(Node x)
{
    int num=0;
    for(int i=0;i<6;i++)
        num=num*10+x.a[i];
    return num;
}
Node turn(Node x,int i)
{
    Node c;
    if(i==1)
    {
        c.a[0]=x.a[5];
        c.a[1]=x.a[4];
        c.a[2]=x.a[2];
        c.a[3]=x.a[3];
        c.a[4]=x.a[0];
        c.a[5]=x.a[1];
    }
    else if(i==2)
    {
        c.a[0]=x.a[4];
        c.a[1]=x.a[5];
        c.a[2]=x.a[2];
        c.a[3]=x.a[3];
        c.a[4]=x.a[1];
        c.a[5]=x.a[0];
    }
    else if(i==3)
    {
        c.a[0]=x.a[3];
        c.a[1]=x.a[2];
        c.a[2]=x.a[0];
        c.a[3]=x.a[1];
        c.a[4]=x.a[4];
        c.a[5]=x.a[5];
    }
    else if(i==4)
    {
        c.a[0]=x.a[2];
        c.a[1]=x.a[3];
        c.a[2]=x.a[1];
        c.a[3]=x.a[0];
        c.a[4]=x.a[4];
        c.a[5]=x.a[5];
    }
    return c;
}
bool judge(Node x)
{
    for(int i=0;i<6;i++)
        if(x.a[i]!=e.a[i])
        return 0;
    return 1;
}
bool vis[2000010];
int bfs()
{
    memset(vis,0,sizeof(vis));
    queue<Node> q;
    vis[hash(s)]=1;
    q.push(s);
    while(!q.empty())
    {
        Node u=q.front();
        q.pop();
        if(judge(u))
            return u.d;
        for(int i=1;i<=4;i++)
        {
            Node v=turn(u,i);
            if(!vis[hash(v)])
            {
                v.d=u.d+1;
                q.push(v);
                vis[hash(v)]=1;
            }
        }
    }
    return -1;
}
int main()
{
    int i;
    while(scanf("%d",&s.a[0])==1)
    {
        for(i=1;i<6;i++)
            scanf("%d",&s.a[i]);
        for(i=0;i<6;i++)
            scanf("%d",&e.a[i]);
        printf("%d\n",bfs());
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值