当前位置:网站首页>[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;
}
边栏推荐
- 【服装软件】服装出产办理体系选型的准则有哪些?
- About SQL: create a view_ XB view, whose function is ① to delete views with duplicate names before creating them ② to display the number of male and female students in this class in the XSB table, and
- [advanced C language] file operation (I)
- Solr基础操作6
- Solr基础操作12
- MySQL foundation 2
- IDEA工具快捷键的使用
- [uitableview] Pit 1: tableview:heightforheaderinsection: method does not execute
- 云原生爱好者周刊:炫酷的 Grafana 监控面板集合
- gyctf_ 2020_ document
猜你喜欢

TwinCAT 3 EL7211模塊控制倍福伺服
![[advanced C language] string and memory function (II)](/img/1a/14ff6a078419e407845d60485be60e.png)
[advanced C language] string and memory function (II)

mysql 死锁

公司固定资产该哪个部门管理,一般公司固定资产怎么管理

JS draw polar color gradient

Five key elements of the data center

《性能之巅第2版》阅读笔记(四)--Memory监测

代码分析平台 SonarQube 实战

IDEA工具快捷键的使用

Introduction to reptiles: data capture of Betta barrage, with 11 introductory notes attached
随机推荐
Solr basic operations 14
Connection query of SQL Server database
Review of vsftp, TFTP, samba and NFS
Solr基础操作14
【UML】UML的几种关系(依赖-关联-聚合-组合-继承-实现)
MySQL primary key constraint deletion
Solr basic operation 6
Solr basic operations 9
Code analysis platform sonarqube actual combat
Solr基础操作12
DataGridView上移 下移行
8 software engineering environment
01背包问题
After 8 years of polishing, "dream factory of game design" released an epic update!
Stack space of JVM
博途V16 更改PLC的型号和固件版本
传统微服务框架如何无缝过渡到服务网格 ASM
MySQL高级篇2
Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)
Solr basic operation 10