hdu 1728 逃离迷宫 bfs

每个点加4个方向

View Code
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

struct node{
    int x,y,dir,step;
    friend bool operator <(node aa, node bb)
    {
        return aa.step>bb.step;
    }
}a;

int n,m,k,x1,y1,x2,y2;
char map[105][105];
int dp[105][105][4];
int d[4][2]={0,1,1,0,0,-1,-1,0};

void init()
{
    int i;
    for (i=0;i<n;i++)scanf("%s",map[i]);
    scanf("%d%d%d%d%d",&k,&y1,&x1,&y2,&x2);
    x1-=1;x2-=1;y1-=1;y2-=1;
    a.dir=-1,a.step=0,a.x=x1,a.y=y1;
    memset(dp,0,sizeof(dp));
}

void bfs()
{
    int i,tx,ty;
    priority_queue<node> q;
    q.push(a);
    while (!q.empty())
    {
        a=q.top();q.pop();
        for (i=0;i<4;i++)
        {
            tx=a.x+d[i][0];
            ty=a.y+d[i][1];
            while (tx>=0&&ty>=0&&tx<n&&ty<m&&map[tx][ty]=='.'&&!dp[tx][ty][i])
            {
                node b;
                b.x=tx;b.y=ty;b.dir=i;b.step=a.step;
                if(i!=a.dir)b.step++;
                if(b.step>k+1)break;
                q.push(b);
                dp[tx][ty][i]=1;
                if(tx==x2&&ty==y2&&b.step<=k+1)
                {
                    printf("yes\n");
                    return;
                }
                tx+=d[i][0];
                ty+=d[i][1];
            }
        }
    }
    printf("no\n");
}

int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&n,&m);
        init();
        bfs();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/104-gogo/archive/2012/11/04/2753491.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值