当前位置:网站首页>走 迷 宫
走 迷 宫
2022-06-25 23:38:00 【Stay--hungry】
题目大意:给定一个 N × M N\times M N×M字符矩阵描述的迷宫(‘#’表示墙壁,‘.’表示可以通行),保证有且仅有一个起点 S S S和终点 E E E。求从起点到终点的最短距离,若无法到达则输出“oop!”。
小技巧:
- 地图的读取方式
char g[N][N];
for (int i = 0; i < n; i ++) cin >> g[i]; //每次读入一个字符串(一维数组)
- 枚举方向
int dx[4] = {
-1, 0, 1, 0}, dy[4] = {
0, 1, 0, -1};
for (int i = 0; i < 4; i ++)
{
int x = t.x + dx[i], y = t.y + dy[i]; //(x, y)为当前所处理的邻接点的坐标,(t.x, t.y)为原点的坐标
...
}
dist数组用于记录每个可达的点到起点的距离,初始化为-1,从而同时也起到st数组的判重作用。
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
int dx[4] = {
-1, 0, 1, 0}, dy[4] = {
0, 1, 0, -1};
const int N = 210;
char g[N][N];
int dist[N][N];
int n, m; //迷宫规模
int bfs(PII start, PII end) //传入起点和终点的坐标
{
memset(dist, -1, sizeof(dist));
queue<PII> q;
q.push(start);
dist[start.x][start.y] = 0;
while (!q.empty())
{
PII t = q.front(); q.pop();
for (int i = 0; i < 4; i ++) //枚举4个方向
{
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= n || y < 0 || y >= m) continue; // 出界
if (g[x][y] == '#') continue; // 障碍物
if (dist[x][y] != -1) continue; // 之前已经遍历过
dist[x][y] = dist[t.x][t.y] + 1;
if (end == make_pair(x, y)) return dist[x][y];
else q.push({
x, y});
}
}
return -1; //迷宫中所有可达的点都被遍历了一次还没有走到终点,说明终点不可达
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++) cin >> g[i];
PII start, end;
for (int i = 0; i < n; i ++) //先找到起点和终点
for (int j = 0; j < m; j ++)
if (g[i][j] == 'S') start = {
i, j};
else if (g[i][j] == 'E') end = {
i, j};
int distance = bfs(start, end);
if (distance == -1) puts("oop!");
else cout << distance;
return 0;
}
边栏推荐
- Black box test - decision table method of test cases
- DGUS新升级:全面支持数字视频播放功能
- C#线程池控制Semaphore
- 远程增量同步神器rsync
- Is it safe for flush software to buy stocks for trading? How to open an account to buy shares
- Containerd client comparison
- About EF page turning query database
- Comment promouvoir efficacement les produits
- Etcd database source code analysis -- inter cluster network layer server interface
- [从零开始学习FPGA编程-44]:视野篇 - 集成电路助力数字化时代高质量发展-1-集成电路芯片主要形态
猜你喜欢

生信周刊第33期

Mpu6050 reads the ID incorrectly and 0xd1 occurs (the correct ID should be 0x68 or 0x69). Solution.

Data analysis slicer, PivotTable and PivotChart (necessary in the workplace)

案例:绘制Matplotlib动态图

Etcd database source code analysis cluster communication initialization

LabVIEW开发监控聚变实验脉冲电源

数据分析——切片器、数据透视表与数据透视图(职场必备)

New library launched | cnopendata China new house information data

jarvisoj_ level2_ x64

ciscn_ 2019_ en_ two
随机推荐
[从零开始学习FPGA编程-44]:视野篇 - 集成电路助力数字化时代高质量发展-1-集成电路芯片主要形态
Radio boxes are mutually exclusive and can be deselected at the same time
halcon之区域:多种区域(Region)生成(4)
马斯克 VS 乔布斯,谁是21世纪最伟大的创业家
经纬度 多点 获取中心点 已解决
Simple deepclone
Is it safe for flush software to buy stocks for trading? How to open an account to buy shares
Flex & Bison 开始
How to effectively promote products
Zhihuijia - full furniture function
STM32 uses SPI mode to drive TFT-LCD optimization code of hx8347 scheme
react + router 框架下的路由跳转后缓存页面存初始参数
如何有效地推廣產品
案例:绘制Matplotlib动态图
SPI protocol
DGUS新升级:全面支持数字视频播放功能
《产品思维30讲》精华及感想
如何有效地推广产品
ETCD数据库源码分析——集群通信初始化
idea配置