当前位置:网站首页>[programming problem] maze problem
[programming problem] maze problem
2022-06-30 00:26:00 【Baldness】
Define a two-dimensional array NM, Such as 55 The array looks like this :
int maze[5][5] = {
0,1,0,0,0,
0,1,1,1,0,
0,0,0,0,0,
0,1,1,1,0
0,0,0,1,0,
};
It means a maze , Among them 1 Wall ,0 Means the way to go , You can only walk sideways or vertically , Don't walk sideways , Ask the programmer to find the route from the upper left corner to the lower right corner , The entry point is [0,0], Since the first grid is the way to go .
Input description :
Enter two integers , Respectively represent the number of rows and columns of a two-dimensional array . Then enter the corresponding array , among 1 Wall ,0 Means the way to go .
Output description :
The shortest path from the top left corner to the bottom right corner , Format example .
Example :
Input :
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 1
0 1 1 1 0
0 0 0 0 0
Output :
(0,0)
(1,0)
(2,0)
(3,0)
(4,0)
(4,1)
(4,2)
(4,3)
(4,4)
We can use backtracking to find the shortest path . The specific steps are: :
First, add the current point to the path , And set it to gone
Judge whether the current point is an exit , If yes, the output path , Save results ; Jump to 4
Judge the top of the current point in turn 、 Next 、 Left 、 Whether the four points on the right can go , If you can go, go to this point recursively
Current point push path , Set to walkable
Specific code
#include<iostream>
#include<vector>
using namespace std;
int N, M; // Represent rows and columns respectively
vector<vector<int>> maze;// Maze matrix
vector<vector<int>> path_temp;// Store the current path , The first dimension represents the position
vector<vector<int>> path_best;// Store the best path
void MazeTrack(int i, int j)
{
maze[i][j] = 1;// Indicates that the current node has left , No more
path_temp.push_back({
i, j });// Add the current node to the path
if (i == N - 1 && j == M - 1) // Judge whether the destination is reached
if (path_best.empty() || path_temp.size() < path_best.size())
path_best = path_temp;
if (i - 1 >= 0 && maze[i - 1][j] == 0)// Explore whether it's possible to go up
MazeTrack(i - 1, j);
if (i + 1 < N && maze[i + 1][j] == 0)// Explore whether it's feasible to go down
MazeTrack(i + 1, j);
if (j - 1 >= 0 && maze[i][j - 1] == 0)// Explore the feasibility of going left
MazeTrack(i, j - 1);
if (j + 1 < M && maze[i][j + 1] == 0)// Explore the feasibility of going right
MazeTrack(i, j + 1);
maze[i][j] = 0; // Restore the scene , Set as not gone
path_temp.pop_back();
}
int main()
{
while (cin >> N >> M)
{
maze = vector<vector<int>>(N, vector<int>(M, 0));
path_temp.clear();
path_best.clear();
for (auto &i : maze)
for (auto &j : i)
cin >> j;
MazeTrack(0, 0);// Trace back to find the shortest path in the maze
for (auto i : path_best)
cout << '(' << i[0] << ',' << i[1] << ')' << endl;// Output path
}
return 0;
}
边栏推荐
- EB-5 immigration in the United States reappears to be positive, and the reauthorization policy of the regional center is suspended
- Quick pow: how to quickly find power
- Code analysis platform sonarqube actual combat
- 公司固定资产该哪个部门管理,一般公司固定资产怎么管理
- MySQL高级篇2
- SQL Server database addition, deletion, modification and query statements
- 代码分析平台 SonarQube 实战
- 请指教在线开户是什么意思?另外想问,现在在线开户安全么?
- 云原生爱好者周刊:炫酷的 Grafana 监控面板集合
- Label Troubleshooting: unable to open the marked image
猜你喜欢

关联性——典型相关分析
![[rust weekly library] Tokei - a utility for statistics of code lines and other information](/img/6c/4569cc0edaa01e4605c9c256193c31.png)
[rust weekly library] Tokei - a utility for statistics of code lines and other information
![[review and Book delivery] 6 interesting R language projects for beginners](/img/d9/b785c92f503b78977b47a7feb88776.jpg)
[review and Book delivery] 6 interesting R language projects for beginners

Sword finger offer II 037 Asteroid collision
![[advanced C language] user defined type](/img/0d/50924ef21f07ca8188855c2b78d929.png)
[advanced C language] user defined type

云原生爱好者周刊:炫酷的 Grafana 监控面板集合

云呐|如何利用系统管理固定资产?如何进行固定资产管理?

学位论文的引用

Can't recognize the original appearance

Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)
随机推荐
01 backpack problem
After 8 years of polishing, "dream factory of game design" released an epic update!
Preliminary syntax of JS
MySQL advanced 1
MySQL advanced 2
面试官:为什么数据库连接很消耗资源?我竟然答不上来。。一下懵了!
MySQL foundation 3
SOFARegistry 源码|数据同步模块解析
Several simple queries of SQL Server database
MySQL高级篇1
Serpentine matrix (array simulates direction, D represents turning)
Relevance - canonical correlation analysis
【毕业季|进击的技术er】工作七年的职场人,不希望你们再走弯路
【PHP】php压测,报错:通常每个套接字地址(协议/网络地址/端口)只允许使用
leetcode-1. Sum of two numbers
How to write controller layer code gracefully?
What does it mean to open an account online? In addition, is it safe to open an account online now?
mysql 死锁
中小企业签署ERP合同时,需要注意这几点
《性能之巅第2版》阅读笔记(四)--Memory监测