当前位置:网站首页>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;
}
边栏推荐
- CityJSON
- Flex & Bison 开始
- **MySQL example 1 (query by multiple conditions according to different problems)**
- 远程增量同步神器rsync
- Duck feeding data instant collection solution resources
- **MySQL例题一(根据不同问题,多条件查询)**
- 生信周刊第33期
- Oracle数据库完全卸载步骤(暂无截图)
- New library launched | cnopendata wholesale price data of agricultural products
- 2022安徽省安全员C证考试练习题模拟考试平台操作
猜你喜欢

Region of Halcon: generation of multiple regions (4)

数组中的第K个最大元素

使用Gin框架运行Demo时报错“ listen tcp :8080: bind: An attempt was made to access a socket in a way forbidden”

超详细SSM框架实现增删改查功能项目整体流程

Etcd database source code analysis cluster communication initialization

Camera - 02 image sensor

Reading notes on how to connect the network - hubs, routers and routers (III)

多接口调用,使用Promise.all、Promise.race和Promise.any

新库上线 | CnOpenDataA股上市公司IPO申报发行文本数据

Case: drawing Matplotlib dynamic graph
随机推荐
毕业季你考虑好去留了吗
Laravel basic course routing and MVC - routing
经纬度 多点 获取中心点 已解决
Classic interview questions: mouse drug test and Hamming code
QT cmake pure C code calls the system console to input scanf and Chinese output garbled code
Have you considered going or staying in graduation season
智慧家——全家具功能
Technical foreword - metauniverse
走 迷 宫
Cartoon shader
MySQL图书借阅系统项目数据库建库表语句(组合主键、外键设置)
信息收集的利器,Google骇客语法
Web信息收集,互联网上的裸奔者
CityJSON
halcon之区域:多种区域(Region)生成(4)
Computer network knowledge summary (interview)
Is it safe to open a fund account? Are there any risks?
生信周刊第33期
完整复习(包含语法)--MYSQL正则表达式
Complete review (including syntax) -- MySQL regular expressions