当前位置:网站首页>Maze walking
Maze walking
2022-06-26 01:31:00 【Stay--hungry】
The main idea of the topic : Given a N × M N\times M N×M The maze described by the character matrix (‘#’ Wall ,‘.’ To show that one can pass through ), There must be only one starting point S S S And the end E E E. Find the shortest distance from the beginning to the end , If not, output “oop!”.
Tips :
- How to read the map
char g[N][N];
for (int i = 0; i < n; i ++) cin >> g[i]; // Read in one string at a time ( One dimensional array )
- Enumeration direction
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) Is the coordinate of the currently processed adjacency point ,(t.x, t.y) Is the coordinate of the origin
...
}
distThe array is used to record the distance from each reachable point to the starting point , Initialize to -1, At the same time, it also plays st Duplicate judgment of array .
#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; // Maze scale
int bfs(PII start, PII end) // Pass in the coordinates of the start and end points
{
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 ++) // enumeration 4 A direction
{
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= n || y < 0 || y >= m) continue; // out
if (g[x][y] == '#') continue; // obstacle
if (dist[x][y] != -1) continue; // It has been traversed before
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; // All the reachable points in the maze have been traversed once and have not reached the end , It means that the end point is unreachable
}
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 ++) // Find the start and end points first
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;
}
边栏推荐
猜你喜欢

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

黑盒测试 — 测试用例 之 判定表法看这一篇就够了

Black box test - decision table method of test cases

Handling of @charset UTF-8 warning problems during vite packaging and construction;

Shell regular expression

SPI protocol

QT cmake pure C code calls the system console to input scanf and Chinese output garbled code

The overall process of adding, deleting, modifying and querying function items realized by super detailed SSM framework

物联网?快来看 Arduino 上云啦

远程增量同步神器rsync
随机推荐
Complete review (including syntax) -- MySQL regular expressions
基金开户安全吗?有没有什么风险?
生信周刊第33期
Template engine - FreeMarker first experience
containerd客户端比较
LabVIEW开发监控聚变实验脉冲电源
Musk vs. jobs, who is the greatest entrepreneur in the 21st century
STM32GPIO
Recognize map
Longitude and latitude multipoint acquisition center point has been solved
Data analysis slicer, PivotTable and PivotChart (necessary in the workplace)
Online gadget sharing (updated from time to time, current quantity: 2)
Camera - 02 image sensor
数字电路——加法器
Quickly generate 1~20 natural numbers and easily copy
2021-1-15 摸魚做的筆記Ctrl+c /v來的
Have you considered going or staying in graduation season
Is it safe to open a fund account? Are there any risks?
The overall process of adding, deleting, modifying and querying function items realized by super detailed SSM framework
Dgus new upgrade: fully support digital video playback function