hdu 1728 逃离迷宫(BFS)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1728

单方向的bfs

要用队头来遍历完所有最优解并且进队,再重复

如果每次让周围四个节点入队,此时在同行、同列还有最优解没进队,导致那些之前没进队的,进队的时候就已经不是最优解了。

#include <cstdio>
#include <queue>
#include <cstring>
using std::queue;

struct node
{
    int x,y,turn;
};
int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
char map[105][105];
int book[105][105];
node nstart,nend;
int main()
{
    int T,m,n,i,j;
    scanf("%d",&T);
    while(T--)
    {
        memset(book, 0, sizeof(book));
        scanf("%d %d",&m,&n);
        for(i = 1; i <= m; ++i)
            for(j = 1; j <= n; ++j)
                scanf(" %c",&map[i][j]);
        int di;
        scanf("%d %d %d %d %d",&di,&nstart.y,&nstart.x,&nend.y,&nend.x);
        book[nstart.x][nstart.y] = 1;
        if(nstart.x == nend.x && nstart.y == nend.y)
        {
            printf("yes\n");
            continue;
        }

        nstart.turn = -1;
        queue<node> que;
        que.push(nstart);
        node cur,pre;
        int flag = 0;
        while(!que.empty())
        {
            pre = que.front();
            que.pop();

            if(pre.x == nend.x && pre.y == nend.y && pre.turn <= di)
            {
                printf("yes\n");
                flag = 1;
                break;
            }

            cur.turn = pre.turn + 1;

            for(int k = 0; k < 4; ++k)
            {
                cur.x = pre.x + next[k][0];
                cur.y = pre.y + next[k][1];

                while(cur.x > 0 && cur.y > 0 && cur.x <= m && cur.y <= n && cur.turn <= di && map[cur.x][cur.y] == '.')
                {
                    if(!book[cur.x][cur.y])
                    {
                        que.push(cur);
                        book[cur.x][cur.y] = 1;
                    }
                    cur.x += next[k][0];
                    cur.y += next[k][1];
                }
            }
        }
        if(!flag)
            printf("no\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值