hdu6171 双向bfs

传送门
题意:给一个金字型塔的数列,第一行一个0,第二行两个1,。。。第6行6个5,现在金字塔数字打乱了,你只能移动0这个数字,只能向左上右上左下右下走,问在20步内能否回到原来状态。
一共21个数,每个有6种状态,没办法用一个 [21][6]维的数组记录状态,所以我们要用Hash记录状态,每个位置6种状态,那么最大是6^21的数字,long long可以存的下的。我们用bfs进行搜索,给两个起点,一个是乱序的起点,一个是原始状态为起点,如果两个状态走到一起了,并且步数小于20,那么就找到结果了,否则无解。

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <algorithm>

using namespace std;
typedef  long long LL;

LL dx[4]= {1,1,-1,-1};
LL dy[4]= {0,1,0,-1};

map<LL,LL>mp[2];
struct node
{
    LL a[7][7];
    LL x,y;
    LL flag;
    LL step;
};
LL Hash(node &t)
{
    LL sta=0;
    for(LL i=1; i<=6; i++)
    {
        for(LL j=1; j<=i; j++)
            sta=sta*6+t.a[i][j];
    }
    return sta;
}
 queue<node>que;
LL bfs(node &s,node &t)
{
    while(!que.empty())
        que.pop();
    s.step=0;///步数
    t.step=0;
    mp[s.flag][Hash(s)]=0;///初始化步数为0
    mp[t.flag][Hash(t)]=0;

    que.push(s);
    que.push(t);
    while(!que.empty())
    {
        node temp;
        node now=que.front();
        que.pop();
        LL temphash=Hash(now);///当前 走到的状态
        if(mp[!now.flag].count(temphash)) /// 看看 反向的 走没走到该状态
            if(mp[!now.flag][temphash]+now.step<=20)/// 看看两个反向走的步数一共是否小于20
                return mp[!now.flag][temphash]+now.step;
            else
                continue;
        if(now.step>=10)///两个方向 最多10步
            continue;
        for(LL i=0;i<4;i++)
        {
            temp=now;
            temp.x=dx[i]+temp.x;
            temp.y=dy[i]+temp.y;
            if(temp.x>=1&&temp.x<=6&&temp.y>=1&&temp.y<=temp.x)
            {
                swap(temp.a[now.x][now.y],temp.a[temp.x][temp.y]);///!!! 交换矩阵里的值
                temphash=Hash(temp);
                if(!mp[temp.flag].count(temphash))///检查走没走过这种状态
                {
                    temp.step++;
                    mp[temp.flag][temphash]=temp.step;
                    que.push(temp);
                }
            }
        }
    }
    return -1;
}
int main()
{
    LL T;
    scanf("%lld",&T);
    while(T--)
    {
        mp[0].clear();///记录乱序出发的路径,<状态,步数>mp[0]
        mp[1].clear();///记录完好顺序出发的路径
        node s,t;
        for(LL i=1; i<=6; i++)
            for(LL j=1; j<=i; j++)
            {
                scanf("%lld",&s.a[i][j]);
                if(s.a[i][j]==0)
                {
                    s.x=i;
                    s.y=j;
                }
                t.a[i][j]=i-1;
            }
        s.flag=0;///flag 表示是哪种顺序,0是乱序出发的,1是完好顺序出发的
        t.flag=1;
        t.x=1;///完好顺序出发的的起始位置为(1,1),我们记录整个矩阵是 从(1,1)到(6,6)
        t.y=1;
        LL temp=bfs(s,t);
        if(temp==-1)
            puts("too difficult");
        else
            printf("%lld\n",temp);
    }
    return 0;
}
/**

0
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5

1
1 2
2 0 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值