hdu 1254

5 篇文章 0 订阅
2 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=1254

 

BFS+hash,广搜路线,每次小人走都要哈希一下箱子的位置,这样保证走的时候状态是不一样的,然后要注意,这里要用优先队列,时间短的要先弹出来,这个地方值得琢磨,WA了n次!

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>

using namespace std;

#define MAXN 12

struct node
{
    int x,y;
    int t;
    int key;
    int px,py;
    node(){};
    node(int x,int y,int px,int py,int t,int key):x(x),y(y),px(px),py(py),t(t),key(key){};
    bool friend operator < (const node &a, const node &b)
    {
        return a.t > b.t;
    }//
};

int map[MAXN][MAXN];
bool mark[MAXN][MAXN][MAXN*MAXN+10];
int val[MAXN][MAXN];
int n,m;
int sx,sy;
int px,py;
priority_queue<node> v;
int dir[][2]={{1,0},{-1,0},{0,1},{0,-1}};

/*bool isconer(node p)
{
    if(p.x==0)
}*/
bool wall(node p)
{
    if(p.x<0||p.x>=n||p.y<0||p.y>=m)
        return true;
    if(map[p.x][p.y]==1)
        return true;
    return false;
}

bool isbox(node p)
{
    if((p.x==p.px)&&(p.y==p.py))
        return true;
    return false;
}

bool wall2(node p)
{
    if(p.px<0||p.px>=n||p.py<0||p.py>=m)
        return true;
    if(map[p.px][p.py]==1)
        return true;
    return false;
}

int BFS()
{
    node p(sx,sy,px,py,0,val[px][py]);
    node temp;
    mark[sx][sy][val[px][py]]=true;
    while(!v.empty())
        v.pop();
    v.push(p);
    while(!v.empty())
    {
        p=v.top();
        v.pop();
//        if(isconer(p))//在角落
//            continue;
        for(int i=0;i<4;i++)
        {
            temp=p;
            temp.x+=dir[i][0];
            temp.y+=dir[i][1];
        //    temp.t++;
            if(wall(temp))//如果是墙
                continue;
            if(isbox(temp))//如果这个位置是箱子
            {
                temp.px+=dir[i][0];
                temp.py+=dir[i][1];//经过移动之后的位置
                temp.t++;
                if(wall2(temp))//箱子被移动到的位置是墙
                    continue;
                if(map[temp.px][temp.py]==3)//箱子被移动到目的位置
                    return temp.t;
                if(mark[temp.x][temp.y][val[temp.px][temp.py]])//如果位置走过了
                    continue;
                mark[temp.x][temp.y][val[temp.px][temp.py]]=true;
                v.push(temp);
                continue;
            }
            if(mark[temp.x][temp.y][val[temp.px][temp.py]])//如果位置走过了
                continue;
            mark[temp.x][temp.y][val[temp.px][temp.py]]=true;
            v.push(temp);
        }
    }
    return -1;
}

int main()
{
    int t;
    int value;
    scanf("%d",&t);
    while(t--)
    {
        value=0;
        px=py=9;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%d",&map[i][j]);
                if(map[i][j]==4)
                {
                    sx=i;
                    sy=j;
                }
                if(map[i][j]==2)
                {
                    px=i;
                    py=j;
                }
                val[i][j]=value++;
            }
        }
        memset(mark,0,sizeof(mark));
        printf("%d\n",BFS());
    }
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值