洛谷: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 1n,m100,且 ( 1 , 1 ) (1,1) (1,1) ( n , m ) (n, m) (n,m) 均为空地。

BFS板子题

#include<bits/stdc++.h>
//#include<iostream>
//#include<iomanip>
//#include<vector>
//#include<queue>
//#include<algorithm>
#define rep(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
#define pii pair<int,int>
#define endl '\n'
const int M=2e5+7;
char a[200][200];
int vis[200][200];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int n,m;
void bfs(int qx,int qy)
{
  queue<pii>q ;
  q.push({qx,qy});
  vis[1][1]=1;
  while(q.size()){
   auto [x,y]=q.front();
     q.pop();
  rep(i,0,3)
   {
     int xx=x+dx[i],yy=y+dy[i];
     if(xx<1 ||xx > n || yy<1||yy>m)continue;
     if(vis[xx][yy] || a[xx][yy]=='#')continue;
    q.push({xx,yy});
     vis[xx][yy]=1;
     
  }
  }
}

int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);

cin>>n>>m;
rep(i,1,n)rep(j,1,m) cin>>a[i][j];
 bfs(1,1);
if(vis[n][m])cout<<"Yes";
else cout<<"No";

return 0;
}

根据提供的信息,B3625+是一道题目,需要使用迷宫寻路算法进行求解。 迷宫寻路算法是一种用于在迷宫中寻找路径的算法。其中,最常用的算法是深度优先搜索和广度优先搜索。在深度优先搜索中,我们从起点开始,沿着一条路径一直走到不能再走为止,然后回溯到前一个节点,继续探索其他路径。在广度优先搜索中,我们从起点开始,先访问所有与起点相邻的节点,然后再访问与这些节点相邻的节点,以此类推,直到找到终点为止。 对于B3625+这道题目,我们可以使用广度优先搜索算法来解决。具体步骤如下: 1.定义一个队列,将起点加入队列中。 2.定义一个visited数组,用于记录每个节点是否被访问过。 3.定义一个pre数组,用于记录每个节点的前驱节点。 4.从队列中取出一个节点,如果该节点是终点,则结束搜索;否则,将该节点的所有未访问过的相邻节点加入队列中,并将这些节点的前驱节点设置为当前节点。 5.重复步骤4,直到找到终点或队列为空。 6.如果找到了终点,则从pre数组中逆推出路径。 下面是使用Python实现的代码: ```python from collections import deque # 定义迷宫 maze = [ [0, 1, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 1, 0] ] # 定义起点和终点 start = (0, 0) end = (4, 4) # 定义方向数组 directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] # 定义队列、visited数组和pre数组 queue = deque() queue.append(start) visited = [[False] * 5 for _ in range(5)] visited[0][0] = True pre = [[None] * 5 for _ in range(5)] # 广度优先搜索 while queue: x, y = queue.popleft() if (x, y) == end: break for dx, dy in directions: nx, ny = x + dx, y + dy if 0 <= nx < 5 and 0 <= ny < 5 and not visited[nx][ny] and maze[nx][ny] == 0: queue.append((nx, ny)) visited[nx][ny] = True pre[nx][ny] = (x, y) # 输出路径 path = [] x, y = end while (x, y) != start: path.append((x, y)) x, y = pre[x][y] path.append(start) path.reverse() print(path) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值