HDU 1728 逃离迷宫(BFS)

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

  • 简单的BFS 题目 ,就是条件之间的逻辑要理清
  • 根据题目,转弯次数不能超过要求
  • 其中 的 吧 VIS 条件 独立出来写,是 为了让所有点扩展出来,而非把VIS的点 当作墙。

  • 为了方便以后复习这种模板题。。我们需要的是

    1. 地图矩阵 2.标记数组 3. 位移数组 4.以及 结构体 5.队列(先进先出)
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

char map[105][105];
int vis[105][105];
int step[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
int m, n,k;
struct Node
{
    int x, y, time;
}Start,End;

int bfs()
{
    Start.time = -1;
    Node now, next;
    queue<Node>q;
    q.push(Start);
    while (!q.empty())
    {
        now = q.front(); q.pop();
        if (now.time >= k) continue;
        for (int i = 0; i < 4; i++)
        {
            next = now;
            next.time++;
            while (1)
            {
                next.x = next.x + step[i][0];
                next.y = next.y + step[i][1];
                if (next.x >= 0 && next.x < m && next.y >= 0 && next.y < n  && map[next.x][next.y] == '.' && next.time <= k)
                {
                    if (next.x == End.x && next.y == End.y) 
                    {
                        return 1;
                    }
                    if (!vis[next.x][next.y]) //该条件 不能放在 上面的IF里面 
                    {
                        q.push(next);
                        vis[next.x][next.y] = 1;
                    }
                }
                else break;
            }

        }
    }
    return 0;
}
int main()
{
    //freopen("debug//in.txt", "r", stdin);
    //freopen("debug//out.txt", "w", stdout);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> m >> n;
        for (int i = 0; i < m;i++)
            for (int j = 0; j < n; j++)
            {
                cin >> map[i][j];
            }
            cin >> k >> Start.y >> Start.x >> End.y >> End.x;
            Start.x--, Start.y--, End.x--, End.y -- ;
            memset(vis, 0, sizeof(vis));
            vis[Start.x][Start.y] = 1;
            if (bfs())
                cout << "yes" << endl;
            else cout << "no" << endl;
    }
    //fclose(stdin);
    //system("pause");
    //fclose(stdout);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值