当前位置:网站首页>Bailian3752 走迷宫【BFS】
Bailian3752 走迷宫【BFS】
2022-04-21 21:14:00 【海岛Blog】
总时间限制: 1000ms 内存限制: 65536kB
描述
一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走;有的格子是空地,可以走。
给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到)。只能在水平方向或垂直方向走,不能斜着走。
输入
第一行是两个整数,R和C,代表迷宫的长和宽。( 1<= R,C <= 40)
接下来是R行,每行C个字符,代表整个迷宫。
空地格子用’.‘表示,有障碍物的格子用’#‘表示。
迷宫左上角和右下角都是’.’。
输出
输出从左上角走到右下角至少要经过多少步(即至少要经过多少个空地格子)。计算步数要包括起点和终点。
样例输入
5 5
…###
#…
#.#.#
#.#.#
#.#…
样例输出
9
问题链接:Bailian3752 走迷宫
问题简述:(略)
问题分析:最短路径问题,用BFS来解决。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* Bailian3752 走迷宫 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#define RC 45
using namespace std;
const int dr[] = {
-1, 1, 0, 0};
const int dc[] = {
0, 0, 1, -1};
int r, c;
char b[RC][RC +1], vis[RC][RC];
struct Node {
int r, c, step;
};
int bfs(int u, int v)
{
Node t = {
u, v, 1};
vis[u][v] = 1;
queue<Node> q;
q.push(t);
while (!q.empty()) {
t = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nextr = t.r + dr[i];
int nextc = t.c + dc[i];
if (nextr < 0 || nextr >= r || nextc < 0 || nextc >= c || vis[nextr][nextc] || b[nextr][nextc] != '.')
continue;
if (nextr == r - 1 && nextc == c - 1)
return t.step + 1;
vis[nextr][nextc] = 1;
q.push({
nextr, nextc, t.step + 1});
}
}
return -1;
}
int main()
{
scanf("%d%d", &r, &c);
for (int i = 0; i < r; i++)
scanf("%s", b[i]);
memset(vis, 0, sizeof(vis));
printf("%d\n", bfs(0, 0));
return 0;
}
版权声明
本文为[海岛Blog]所创,转载请带上原文链接,感谢
https://tigerisland.blog.csdn.net/article/details/124267960
边栏推荐
- Manuel d'utilisation et de développement de la plate - forme de connexion unique pour l'amarrage du système d'AP de Tongda
- 7-3 simple simulation of banking business queue | PTA
- Tongda OA system docking single sign on platform use and Development Manual
- win10使用技巧之关闭软件安装前的用户提示
- 通道满了 继续往里面发 会如何?
- 档案管理系统操作说明
- Sword finger offer 15 Number of 1 in binary
- PR视频添加字幕
- 其它——ZeroRPC和SimpleXMLRPCServer
- 红日靶场--内网渗透练习
猜你喜欢

10分钟快速入门RDS

What is the most important aspect of slip ring wiring

其它-Supervisor的使用
![[embedded] about IAP + XMODEM receiving bin file from outside to upgrade the chip](/img/05/c69e3701bf80f03c8ec35c033944b1.png)
[embedded] about IAP + XMODEM receiving bin file from outside to upgrade the chip

Operation instructions for upgrading Tongda OA workflow

滑环接线最主要的看什么

Others - MYCAT realizes sub database and sub table

Principal component analysis R language implementation

The annual salary is 170W. Alibaba P8 blind date requires the woman's monthly salary of 10000. Netizen: it's a little high

HMS Core 6.4.0版本发布公告
随机推荐
Importance of slip ring technology in machine operation
使用 Helm 部署 Wikijs
通达OA与第三方APP对接
Principal component analysis R language implementation
其它——Redis与Mysql双写一致性方案解析
基于OpenStack的云计算平台搭建
SAP那些事-职业篇-36-从“固定资产清理”科目说开去
gstreamer学习
Cross compile C program for rk3568 development board
Future development of manufacturing industry after digital transformation
uart学习
Tips for using win10 close user tips before software installation
Wiki.js 配置 LDAP 认证
OA表单设计 案例展示
HMS Core 6.4.0版本发布公告
学生管理系统的架构文档
[SQL] sql22 counts the number of salary records of each department
【常用英文单词】
Others - use of siege pressure test tool
Fundamentals of reinforcement learning (I): multi armed bandit