hdu 5012 Dice 记忆化搜索

本文深入探讨了解决两个不同骰子匹配问题的算法优化与具体实现细节,通过采用广度优先搜索(BFS)和记忆化策略,有效减少了计算步骤并提高了效率。文中详细解释了如何通过四种基本旋转操作使骰子面朝一致,并通过实例展示了算法的正确应用及性能优化。此外,还提供了一个包含输入输出样例的代码片段,旨在帮助读者理解和实现该算法。

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

Dice

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


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

把骰子的初始状态记录下来。步数初始化为0;

这样不停得按他所说的四种转法记忆化+bfs就行了。 一般这种搜步数的我都用bfs,不知道dfs行不行。

中间在结构体中打的是括号的重载运算符操作。不知道为什么结构体中用数组不能用这个方法来构造。

size记录下步数,遇到重点状态,然后把size输出出来就好了。


#include<stdio.h>
#include<queue>
using namespace std;
 
struct saizi
{
 
    int b1,b2,b3,b4,b5,b6;
    int size;
    saizi() {}
    saizi(int a1,int a2,int a3,int a4,int a5,int a6,int si):b1(a1),b2(a2),b3(a3),b4(a4),b5(a5),b6(a6),size(si){};
    /*saizi(int a1,int a2,int a3,int a4,int a5,int a6,int si)
    {
        b1=a1;
        a[2]=a2;
        a[3]=a3;
        a[4]=a4;
        a[5]=a5;
        a[6]=a6;
        size=si;
    }*/
    bool operator == (const saizi& b) const //  如果 是普通的优先队列 运算符是<
    {
        if(b.b1==b1&&b.b2==b2&&b.b3==b3&&b.b4==b4&&b.b5==b5&&b.b6==b6)
            return 1;
        return 0;
    }
}sai,tem,tel;
int dice[10][10][10][10][10][10];
int a[10];
int bfs()
{
    //if(dice[a[n][1]][a[n][2]][a[n][3]][a[n][4]][a[n][5]][a[n][6]])
    //return ;
    queue<saizi> q;
    sai.size=0;
    q.push(sai);
    while(!q.empty())
    {
        sai=q.front();
        q.pop();
        if(sai==tel)
            return sai.size;
        if(dice[sai.b1][sai.b2][sai.b3][sai.b4][sai.b5][sai.b6]==1)
            continue;
        dice[sai.b1][sai.b2][sai.b3][sai.b4][sai.b5][sai.b6]=1;
         //左为轴
        //shun1    
        tem=saizi(sai.b6,sai.b5,sai.b3,sai.b4,sai.b1,sai.b2,sai.size+1);
        q.push(tem);
        
        //shun2
        //tem=saizi(sai.b2,sai.b1,sai.b3,sai.b4,sai.b6,sai.b5,sai.size+1);
        //q.push(tem);
        //shun3
        tem=saizi(sai.b5,sai.b6,sai.b3,sai.b4,sai.b2,sai.b1,sai.size+1);
        q.push(tem);
        


        //前为轴
        //shun1
        tem=saizi(sai.b3,sai.b4,sai.b2,sai.b1,sai.b5,sai.b6,sai.size+1);
        q.push(tem);
        
        //shun2
        //tem=saizi(sai.b2,sai.b1,sai.b4,sai.b3,sai.b5,sai.b6,sai.size+1);
        //q.push(tem);
        //shun3
        tem=saizi(sai.b4,sai.b3,sai.b1,sai.b2,sai.b5,sai.b6,sai.size+1);
        q.push(tem);
        


        //上为轴
        //顺1
        //tem=saizi(sai.b1,sai.b2,sai.b5,sai.b6,sai.b4,sai.b3,sai.size+1);
        //q.push(tem);
        /*
        //shun2
        tem=saizi(sai.b1,sai.b2,sai.b3,sai.b4,sai.b6,sai.b5,sai.size+1);
        q.push(tem);
        //shun3
        tem=saizi(sai.b1,sai.b2,sai.b6,sai.b5,sai.b3,sai.b4,sai.size+1);
        q.push(tem);
        */


    }
    return -1;
}

int main()
{
    int ans;
    while(scanf("%d",&sai.b1)!=EOF)
    {
        memset(dice,0,sizeof(dice));
 
        scanf("%d",&sai.b2);
        scanf("%d",&sai.b3);
        scanf("%d",&sai.b4);
        scanf("%d",&sai.b5);
        scanf("%d",&sai.b6);

     
        scanf("%d",&tel.b1);
        scanf("%d",&tel.b2);
        scanf("%d",&tel.b3);
        scanf("%d",&tel.b4);
        scanf("%d",&tel.b5);
        scanf("%d",&tel.b6);
        
 
        ans=bfs();
        printf("%d\n",ans);
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值