【搜索】 HDU 5012 Dice

本文介绍了如何通过编程解决骰子转动时,从一个面转到另一个面的问题,包括算法设计与实现过程。

无脑搜

next存下骰子转动 哪个面转到哪个面的四种方向

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <math.h>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 130;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 1101521204;
const int mod = 10000007;
int star[6],end[6];
bool vis[7][7][7][7][7][7];
int next[4][6]={3,2,0,1,4,5
,2,3,1,0,4,5,5,4,2,3,0,1,4,5,2,3,1,0};
struct node
{
    int no[6];
    int step;
};
queue<node>q;
int bfs()
{
    node fre,rear;
    while(!q.empty())
        q.pop();
    int sum=0;
    for(int i=0;i<6;i++) if(star[i]==end[i]) sum++;
    if(sum==6) return 0;
    fre.step=0;
    for(int i=0;i<6;i++) fre.no[i]=star[i];
    q.push(fre);
    while(!q.empty())
    {
        fre=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int y[6];
            for(int j=0;j<6;j++)
                y[j]=fre.no[next[i][j]];
            if(!vis[y[0]][y[1]][y[2]][y[3]][y[4]][y[5]])
            {
                vis[y[0]][y[1]][y[2]][y[3]][y[4]][y[5]]=true;
                sum=0;
                for(int j=0;j<6;j++) if(y[j]==end[j]) sum++;
                if(sum==6)return fre.step+1;
                else {
                    for(int j=0;j<6;j++) rear.no[j]=y[j];
                    rear.step=fre.step+1;
                    q.push(rear);
                }
            }
        }
    }
    return -1;
}
int main()
{
    //IN;
    while(scanf("%d",&star[0])!=EOF)
    {
        cler(vis,false);
        for(int i=1;i<6;i++)
            scanf("%d",&star[i]);
        for(int i=0;i<6;i++)
            scanf("%d",&end[i]);
        printf("%d\n",bfs());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值