【NC15434】wyh的迷宫

本文介绍了使用C++编程语言实现深度优先搜索(DFS)和广度优先搜索(BFS)算法解决迷宫问题的两种思路,通过实例展示了如何在迷宫中从起点S找到终点T,避开障碍物X。
摘要由CSDN通过智能技术生成

题目

wyh的迷宫

深搜和广搜模板题

思路

思路一:深度优先搜索

从起点开始,不撞南墙不回头,不走到不能走的位置就不换方向

思路二:广度优先搜索

从起点开始,一步一步地往外探索,一层一层地剥洋葱

代码

// 思路一:深搜
#include <functional>
#include <iostream>
using namespace std;

const char S = 's';  // 起点
const char T = 't';  // 终点
const char X = 'x';  // 障碍物

const int N = 502;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    char maze[N][N];
    int t = 0, n = 0, m = 0;
    function<bool(int, int)> dfs = [&](int xx, int yy) -> bool {
        if (xx < 0 || xx >= n || yy < 0 || yy >= m || maze[xx][yy] == X)
            return false;
        if (maze[xx][yy] == T) return true;
        maze[xx][yy] = X;
        return dfs(xx - 1, yy) || dfs(xx, yy - 1) || dfs(xx + 1, yy) ||
               dfs(xx, yy + 1);
    };
    int i = 0, j = 0, x = 0, y = 0;
    cin >> t;
    while (t--) {
        cin >> n >> m;
        for (i = 0; i < n; i++) {
            for (j = 0; j < m; j++) {
                cin >> maze[i][j];
                if (maze[i][j] == S) {
                    x = i, y = j;
                }
            }
        }
        cout << (dfs(x, y) ? "YES" : "NO") << endl;
    }
    return 0;
}
// 思路二:广搜
#include <iostream>
using namespace std;

const char S = 's';  // 起点
const char T = 't';  // 终点
const char X = 'x';  // 障碍物

const int N = 502;
const int TO[][2]{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};

int main(void) {
	// 加上这两句解绑,运行速度会快一倍
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 0, n = 0, m = 0;
    char maze[N][N];
    int q[N * N], h = -1, r = -1;  // 数组模拟队列,主要是方便清空
    int i = 0, j = 0, x = 0, y = 0, tx = 0, ty = 0;
    auto check = [&] {
        h = r = -1;  // 清空队列
        q[++r] = x * 1000 + y;
        maze[x][y] = X;
        while (h < r) {
            x = q[h + 1] / 1000;
            y = q[h + 1] % 1000;
            h++;
            for (i = 0; i < 4; i++) {
                tx = x + TO[i][0];
                ty = y + TO[i][1];
                if (tx < 0 || tx >= n || ty < 0 || ty >= m ||
                    maze[tx][ty] == X) {
                    continue;
                }
                if (maze[tx][ty] == T) return true;
                maze[tx][ty] = X;
                q[++r] = tx * 1000 + ty;
            }
        }
        return false;
    };
    cin >> t;
    while (t--) {
        cin >> n >> m;
        for (i = 0; i < n; i++) {
            for (j = 0; j < m; j++) {
                cin >> maze[i][j];
                if (maze[i][j] == S) {
                    x = i, y = j;
                }
            }
        }
        cout << (check() ? "YES" : "NO") << endl;
    }
    return 0;
}
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木又可可

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值