DFS 算法:洛谷B3625迷宫寻路

我的个人主页 {\large \mathsf{{\color{Red} 我的个人主页} } } 我的个人主页

往 {\color{Red} {\Huge 往} } 往 期 {\color{Green} {\Huge 期} } 期 文 {\color{Blue} {\Huge 文} } 文 章 {\color{Orange} {\Huge 章}} 章


此系列更新频繁 ,求各位读者点赞
关注我可以了解更多内容哦
偷偷告诉你,我正在冲 1100 粉 {\color{Gray} {\small 偷偷告诉你,我正在冲1100粉} } 偷偷告诉你,我正在冲1100粉
你们有什么想了解的可以发在评论区,我会仔细的看 {\color{Gray} {\small 你们有什么想了解的可以发在评论区,我会仔细的看} } 你们有什么想了解的可以发在评论区,我会仔细的看
1100 粉时我会抓几个做文章 {\color{Gray} {\small1100粉时我会抓几个做文章} } 1100粉时我会抓几个做文章


目录

今天我们学什么

今天我们不学什么,就是做一道二维DFS的题目

例题

我们选用了洛谷题目:B3625 迷宫寻路

题目描述

机器猫被困在一个矩形迷宫里。

迷宫可以视为一个 n × m n\times m n×m 矩阵,每个位置要么是空地,要么是墙。机器猫只能从一个空地走到其上、下、左、右的空地。

机器猫初始时位于 ( 1 , 1 ) (1, 1) (1,1) 的位置,问能否走到 ( n , m ) (n, m) (n,m) 位置。

输入格式

第一行,两个正整数 n , m n,m n,m。

接下来 n n n 行,输入这个迷宫。每行输入一个长为 m m m 的字符串,# 表示墙,. 表示空地。

输出格式

仅一行,一个字符串。如果机器猫能走到 ( n , m ) (n, m) (n,m),则输出 Yes;否则输出 No

样例 #1

样例输入 #1

复制代码
3 5
.##.#
.#...
...#.

样例输出 #1

复制代码
Yes

提示

样例解释

路线如下: ( 1 , 1 ) → ( 2 , 1 ) → ( 3 , 1 ) → ( 3 , 2 ) → ( 3 , 3 ) → ( 2 , 3 ) → ( 2 , 4 ) → ( 2 , 5 ) → ( 3 , 5 ) (1,1)\to (2,1) \to (3,1) \to (3,2)\to (3,3) \to (2, 3) \to (2, 4) \to (2, 5) \to (3, 5) (1,1)→(2,1)→(3,1)→(3,2)→(3,3)→(2,3)→(2,4)→(2,5)→(3,5)

数据规模与约定

对于 100 % 100\% 100% 的数据,保证 1 ≤ n , m ≤ 100 1 \leq n, m \leq 100 1≤n,m≤100,且 ( 1 , 1 ) (1,1) (1,1) 和 ( n , m ) (n, m) (n,m) 均为空地。



题解

错误示范

我们第一时间可以想到,这是一道简单的二维DFS题目,随便一些就能AC,这大概是你想到的代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
char a[105][105];
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
int n,m;
bool vis[105][105];
void dfs(int x,int y)
{
	if(x<1 || x>n || y<1 || y>m || a[x][y]=='#')
	{
		return; 
	}
	if(x==n && y==m)
	{
		cout<<"Yes";
		exit(0);
	}
	
	for(int i=0; i<=3; i++)
	{
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(!vis[tx][ty])
		{
			vis[tx][ty]=1;
			dfs(tx,ty);
			vis[tx][ty]=0;
		}
	}
}

int main()
{
	cin>>n>>m;
	for(int i=1; i<=n ;i++)
	{
		string s;
		cin>>s;
		for(int j=0; j<m; j++)
		{
			a[i][j+1]=s[j];
		}
	}
	dfs(1,1);
	cout<<"No";
	return 0;
}

但是这样你只能得到10分

此时,我们可以想一想,你为什么需要回溯呢?

删除vis[tx][ty]=0;即可AC

正确代码

为了方便阅读,我直接给出了正确代码(我做人太好了

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
char a[105][105];
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
int n,m;
bool vis[105][105];
void dfs(int x,int y)
{
	if(x<1 || x>n || y<1 || y>m || a[x][y]=='#')
	{
		return; 
	}
	if(x==n && y==m)
	{
		cout<<"Yes";
		exit(0);
	}
	
	for(int i=0; i<=3; i++)
	{
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(!vis[tx][ty])
		{
			vis[tx][ty]=1;
			dfs(tx,ty);
		}
	}
}

int main()
{
	cin>>n>>m;
	for(int i=1; i<=n ;i++)
	{
		string s;
		cin>>s;
		for(int j=0; j<m; j++)
		{
			a[i][j+1]=s[j];
		}
	}
	dfs(1,1);
	cout<<"No";
	return 0;
}

怎么样,这是不是很简单呢?

总结

做题要思考!!!

相关推荐
方案开发PCBA抄板芯片解密13 分钟前
什么是算法:高效解决问题的逻辑框架
算法
songx_9924 分钟前
leetcode9(跳跃游戏)
数据结构·算法·游戏
小白狮ww1 小时前
RStudio 教程:以抑郁量表测评数据分析为例
人工智能·算法·机器学习
AAA修煤气灶刘哥1 小时前
接口又被冲崩了?Sentinel 这 4 种限流算法,帮你守住后端『流量安全阀』
后端·算法·spring cloud
kk”2 小时前
C语言快速排序
数据结构·算法·排序算法
纪元A梦2 小时前
贪心算法应用:基因编辑靶点选择问题详解
算法·贪心算法
3壹2 小时前
数据结构精讲:栈与队列实战指南
c语言·开发语言·数据结构·c++·算法
skytier3 小时前
Construct内报错和定位解决
算法
skytier3 小时前
Ascend print数据落盘使用
算法
etcix3 小时前
dmenux.c: integrate dmenu project as one file
c语言·前端·算法