HDU 5012 Dice (BFS)


其实是很水的一道bfs,用字符串表示每个状态,map判重就ok了。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5012

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<string>
#include<queue>
#include<map>
#define N 7
using namespace std;

string a,b;
map<string,int> Hash;
struct node
{
    int t;
    string v;
};
int rot[4][7]={     //Rotation operation
    {0,4,3,1,2,5,6},    //left
    {0,3,4,2,1,5,6},    //right
    {0,6,5,3,4,1,2},    //front
    {0,5,6,3,4,2,1},    //back
};
int bfs()
{
    queue<node> q;
    node u,v;
    u.t=0; u.v=b;
    q.push(u);
    Hash[b]=1;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        if(u.v==a)
            return u.t;
        for(int i=0;i<4;i++)
        {
            v.v="";
            for(int j=1;j<7;j++)
                v.v+=u.v[rot[i][j]-1]; //~~
            if(!Hash[v.v])
            {
                v.t=u.t+1;
                q.push(v);
                Hash[v.v]=1;
            }
        }
    }
    return -1;
}
int main()
{
    string t;
    while(cin>>t)
    {
        a=b="";
        a+=t;
        for(int i=2;i<=6;i++)
        {
            cin>>t;
            a+=t;
        }
        for(int j=1;j<=6;j++)
        {
            cin>>t;
            b+=t;
        }
        Hash.clear();
        cout<<bfs()<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值