当前位置: 首页 > news >正文

1112. 迷宫(DFS之连通性模型)

1112. 迷宫 - AcWing题库

一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由 n∗n 的格点组成,每个格点只有2种状态,.#,前者表示可以通行后者表示不能通行。

同时当Extense处在某个格点时,他只能移动到东南西北(或者说上下左右)四个方向之一的相邻格点上,Extense想要从点A走到点B,问在不走出迷宫的情况下能不能办到。

如果起点或者终点有一个不能通行(为#),则看成无法办到。

注意:A、B不一定是两个不同的点。

输入格式

第1行是测试数据的组数 k,后面跟着 k 组输入。

每组测试数据的第1行是一个正整数 n,表示迷宫的规模是 n∗n 的。

接下来是一个 n∗n 的矩阵,矩阵中的元素为.或者#

再接下来一行是 4 个整数 ha,la,hb,lb,描述 A 处在第 ha 行, 第 la 列,B 处在第 hb 行, 第 lb 列。

注意到 ha,la,hb,lb 全部是从 0 开始计数的。

输出格式

k行,每行输出对应一个输入。

能办到则输出“YES”,否则输出“NO”。

数据范围

1≤n≤100

输入样例:
2
3
.##
..#
#..
0 0 2 2
5
.....
###.#
..#..
###..
...#.
0 0 4 0
输出样例:
YES
NO

解析 :

使用dfs进行判断代码要比bfs简洁

dfs代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<sstream>
#include<deque>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int N = 1e2 + 2;
int n, ha, la, hb, lb;
char str[N][N];
bool vis[N][N];
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
bool dfs(int x, int y) {if (str[x][y] == '#')return false;if (x == hb && y == lb)return true;for (int i = 0; i < 4; i++) {int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= n || b < 0 || b >= n)continue;if (vis[a][b])continue;vis[a][b] = 1;if (dfs(a, b))return true;}return false;
}int main() {int T;cin >> T;while (T--) {cin >> n;for (int i = 0; i < n; i++) {scanf("%s", str[i]);}cin >> ha >> la >> hb >> lb;memset(vis, 0, sizeof vis);if (dfs(ha, la))cout << "YES" << endl;else cout << "NO" << endl;}return 0;
}

BFS代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<sstream>
#include<deque>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int N = 1e2 + 2;
int n,ha,la,hb,lb;
char str[N][N];
typedef pair<int, int> PII;
bool vis[N][N];string bfs() {string ret1 = "YES", ret2 = "NO";if (str[ha][la] == '#' || str[hb][lb] == '#')return ret2;int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };memset(vis, 0, sizeof vis);queue<PII>q;q.push({ ha,la });vis[ha][la] = 1;while (!q.empty()) {auto t = q.front();q.pop();if (t.first == hb && t.second == lb)return ret1;for (int i = 0; i < 4; i++) {int a = t.first + dx[i], b = t.second + dy[i];if (a < 0 || a >= n || b < 0 || b >= n)continue;if (str[a][b] == '#'||vis[a][b])continue;vis[a][b] = 1;q.push({ a,b });}}return ret2;
}int main() {int T;cin >> T;while (T--) {cin >> n;for (int i = 0; i < n; i++) {scanf("%s", str[i]);}cin >> ha >> la >> hb >> lb;cout << bfs() << endl;}return 0;
}

http://www.lryc.cn/news/263869.html

相关文章:

  • 飞天使-k8s知识点1-kubernetes架构简述
  • linux中deadline调度原理与代码注释
  • jquery、vue、uni-app、小程序的页面传参方式
  • ModuleNotFoundError: No module named ‘openai.error‘
  • 理解pom.xml中的parent标签
  • element ui el-avatar 源码解析零基础逐行解析
  • Linux下c语言实现动态库的动态调用
  • 为什么MCU在ADC采样时IO口有毛刺?
  • C# 将 Word 转化分享为电子期刊
  • 网络世界的黑暗角落:常见漏洞攻防大揭秘
  • 通信领域发展方向
  • 21 3GPP中 5G NR高速列车通信标准化
  • 【网络安全】-Linux操作系统—CentOS安装、配置
  • CCNP课程实验-OSPF-CFG
  • 【Spring Security】打造安全无忧的Web应用--入门篇
  • 【每日一题】【12.20】2828.判别首字母缩略词
  • LabVIEW开发振动数据分析系统
  • 去掉乘法运算的加法移位神经网络架构
  • 【TB作品】51单片机,具有报时报温功能的电子钟
  • 了解C++工作机制
  • 力扣题目学习笔记(OC + Swift) 14. 最长公共前缀
  • WinSW设置应用程序开机启动
  • Leetcode—96.不同的二叉搜索树【中等】
  • 正则表达式零宽断言
  • uni-app学习记录
  • API资源对象StorageClass;Ceph存储;搭建Ceph集群;k8s使用ceph
  • Databend 开源周报第 124 期
  • Arduino开发实例-液体流量测量
  • 【idea】解决sprintboot项目创建遇到的问题
  • ADC芯片CS1237在电子秤方案的优势