Dice - HDU 5012 搜索

探讨了通过四种特定操作将两个特殊骰子从不同初始状态变为完全相同状态所需的最少步骤数。采用广度优先搜索算法解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dice

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


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
 

题意:一次翻转有4种方式,问你将骰子的一种方式变成另一种方式至少需要多少步。

思路:广搜就可以了。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
int vis[150000],vc[810][1000][7];
int t,pow7[10],num[7],ans,op;
bool flag;
int suan(int pos)
{
    int i,k=0;
    for(i=1;i<=6;i++)
       k+=num[i]*pow7[i];
    if(k==op)
    {
        flag=true;
        ans=pos+1;
    }
    if(vis[k]==t)
      return 0;
    else
    {
        vis[k]=t;
        return 1;
    }
}
void solve(int pos,int num2)
{
    if(flag)
      return;
    int i,j,k;
    //11111111111
    num[1]=vc[pos][num2][3];
    num[2]=vc[pos][num2][4];
    num[3]=vc[pos][num2][2];
    num[4]=vc[pos][num2][1];
    num[5]=vc[pos][num2][5];
    num[6]=vc[pos][num2][6];

    k=suan(pos);
    if(k==1)
    {
        vc[pos+1][0][0]++;
        k=vc[pos+1][0][0];
        for(i=1;i<=6;i++)
           vc[pos+1][k][i]=num[i];
    }
    num[1]=vc[pos][num2][5];
    num[2]=vc[pos][num2][6];
    num[3]=vc[pos][num2][3];
    num[4]=vc[pos][num2][4];
    num[5]=vc[pos][num2][2];
    num[6]=vc[pos][num2][1];
    k=suan(pos);
    if(k==1)
    {
        vc[pos+1][0][0]++;
        k=vc[pos+1][0][0];
        for(i=1;i<=6;i++)
           vc[pos+1][k][i]=num[i];
    }
    num[1]=vc[pos][num2][6];
    num[2]=vc[pos][num2][5];
    num[3]=vc[pos][num2][3];
    num[4]=vc[pos][num2][4];
    num[5]=vc[pos][num2][1];
    num[6]=vc[pos][num2][2];

    k=suan(pos);
    if(k==1)
    {
        vc[pos+1][0][0]++;
        k=vc[pos+1][0][0];
        for(i=1;i<=6;i++)
           vc[pos+1][k][i]=num[i];
    }
    num[1]=vc[pos][num2][4];
    num[2]=vc[pos][num2][3];
    num[3]=vc[pos][num2][1];
    num[4]=vc[pos][num2][2];
    num[5]=vc[pos][num2][5];
    num[6]=vc[pos][num2][6];

    k=suan(pos);
    if(k==1)
    {
        vc[pos+1][0][0]++;
        k=vc[pos+1][0][0];
        for(i=1;i<=6;i++)
           vc[pos+1][k][i]=num[i];
    }
}
int main()
{
    int n,m,i,j,k;
    pow7[1]=1;
    for(i=2;i<=6;i++)
       pow7[i]=pow7[i-1]*7;
    while(~scanf("%d",&vc[0][1][1]))
    {
        t++;
        for(i=2;i<=6;i++)
           scanf("%d",&vc[0][1][i]);
        vc[0][0][0]=1;

        k=0;
        for(i=1;i<=6;i++)
           scanf("%d",&num[i]);
        for(i=1;i<=6;i++)
           k+=num[i]*pow7[i];
        op=k;


        k=0;
        for(i=1;i<=6;i++)
           k+=vc[0][1][i]*pow7[i];
        if(k==op)
        {
            printf("0\n");
            continue;
        }

        for(i=1;i<=800;i++)
           vc[i][0][0]=0;
        flag=false;
        for(i=0;i<=800;i++)
        {
            //printf("solve %d\n",i);
            if(flag ||vc[i][0][0]==0)
              break;
            for(j=1;j<=vc[i][0][0];j++)
            {
                if(flag)
                  break;
                solve(i,j);
            }
        }
        if(flag)
          printf("%d\n",ans);
        else
          printf("-1\n");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值