当前位置:网站首页>走 迷 宫
走 迷 宫
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;
}
边栏推荐
猜你喜欢

JS逆向案例:破解登录密码

Motor monitoring system based on MCGS and stm32

Endnote IEEE Transactions on industrial electronics/tie/tpel reference format template

New library launched | cnopendata China new house information data

Summary of push-pull output and open drain output of STM32 and failure of analog IIC driving mlx90615

Modelsim simulation FFT core cannot be simulated solution (qsys)

The kth largest element in the array

Nacos registry

Black box test - decision table method of test cases

Music spectrum display toy -- implementation and application of FFT in stm32
随机推荐
Musk vs. jobs, who is the greatest entrepreneur in the 21st century
超详细SSM框架实现增删改查功能项目整体流程
QT cmake pure C code calls the system console to input scanf and Chinese output garbled code
The maze of God's perspective in robot vision
信息收集的利器,Google骇客语法
ciscn_ 2019_ en_ two
Mpu6050 reads the ID incorrectly and 0xd1 occurs (the correct ID should be 0x68 or 0x69). Solution.
Etcd database source code analysis -- inter cluster network layer server interface
Installation and startup of redis
Inheritance -- holy grail mode
Endnote IEEE TRANSACTIONS ON INDUSTRIAL ELECTRONICS/TIE/TPEL 参考文献格式模板
Recognize map
. Net using access 2010 database
ETCD数据库源码分析——集群间网络层服务端接口
Idea configuration
黑盒测试 — 测试用例 之 判定表法看这一篇就够了
Duck feeding data instant collection solution resources
Technical introduction - detailed explanation of chip manufacturing process
剑指 Offer II 096. 字符串交织
Solve STM32 operation μ Solution to sudden failure of task scheduling in c/os-ii system