当前位置:网站首页>【编程题】迷宫问题
【编程题】迷宫问题
2022-06-30 00:23:00 【秃头宇】
定义一个二维数组NM,如55数组如下所示:
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,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或者竖着走,不能斜着走,要求编程序找出从左上角到右下角的路线,入口点为[0,0],既第一格是能走的路。
输入描述:
输入两个整数,分别表示二维数组的行和列数。再输入相应的数组,其中1表示墙壁,0表示可以走的路。
输出描述:
左上角到右下角的最短路径,格式样例所示。
示例:
输入:
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
输出:
(0,0)
(1,0)
(2,0)
(3,0)
(4,0)
(4,1)
(4,2)
(4,3)
(4,4)
我们可以使用回溯法的方式实现寻找最短路径。具体步骤为:
首先将当前点加入路径,并设置为已走
判断当前点是否为出口,是则输出路径,保存结果;跳转到4
依次判断当前点的上、下、左、右四个点是否可走,如果可走则递归走该点
当前点推出路径,设置为可走
具体代码
#include<iostream>
#include<vector>
using namespace std;
int N, M; //分别代表行和列
vector<vector<int>> maze;//迷宫矩阵
vector<vector<int>> path_temp;//存储当前路径,第一维表示位置
vector<vector<int>> path_best;//存储最佳路径
void MazeTrack(int i, int j)
{
maze[i][j] = 1;//表示当前节点已走,不可再走
path_temp.push_back({
i, j });//将当前节点加入到路径中
if (i == N - 1 && j == M - 1) //判断是否到达终点
if (path_best.empty() || path_temp.size() < path_best.size())
path_best = path_temp;
if (i - 1 >= 0 && maze[i - 1][j] == 0)//探索向上走是否可行
MazeTrack(i - 1, j);
if (i + 1 < N && maze[i + 1][j] == 0)//探索向下走是否可行
MazeTrack(i + 1, j);
if (j - 1 >= 0 && maze[i][j - 1] == 0)//探索向左走是否可行
MazeTrack(i, j - 1);
if (j + 1 < M && maze[i][j + 1] == 0)//探索向右走是否可行
MazeTrack(i, j + 1);
maze[i][j] = 0; //恢复现场,设为未走
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);//回溯寻找迷宫最短通路
for (auto i : path_best)
cout << '(' << i[0] << ',' << i[1] << ')' << endl;//输出通路
}
return 0;
}
边栏推荐
- QT learning 02 GUI program example analysis
- gyctf_2020_document
- [QNX Hypervisor 2.2用户手册]6.2.2 Guest与Host之间通信
- DOM 知识点总结
- Preliminary syntax of JS
- [advanced C language] file operation (II)
- 简要的说一下:Fragment 间的通信方式?
- QT learning 06 widgets and window types
- Solr基础操作16
- Majority element ii[molar voting method for finding modes]
猜你喜欢

Vulnhub target -moriartycorp

QT learning 01 GUI program principle analysis

Introduction to reptiles: data capture of Betta barrage, with 11 introductory notes attached

Serpentine matrix (array simulates direction, D represents turning)

After 8 years of polishing, "dream factory of game design" released an epic update!

Activity invitation | the Apache Doris community essay and speech solicitation activity has begun!

GET 和 POST请求的本质区别是什么?

New CorelDRAW technical suite2022 latest detailed function introduction

vsftp 与 TFTP 与 samba 与 nfs 复习

Quick Pow: 如何快速求幂
随机推荐
Serpentine matrix (array simulates direction, D represents turning)
SOFARegistry 源码|数据同步模块解析
代码分析平台 SonarQube 实战
C MDI open subform to remove automatically generated menu bar
视频ToneMapping(HDR转SDR)中的颜色空间转换问题(BT2020转BT709,YCbCr、YUV和RGB)
间歇采样转发干扰
JS绘制极坐标颜色渐变
Quick Pow: 如何快速求幂
Solr basic operation 6
Three postures of anti CSRF blasting
Clone undirected graph [bfs accesses each edge but not only nodes]
固定资产管理系统多少钱,固定资产管理系统价格
Can't recognize the original appearance
MySQL基础篇1
Analysis of common vlog parameters
New CorelDRAW technical suite2022 latest detailed function introduction
QT learning 07 coordinate system in QT
Basic operations such as MySQL startup under Windows platform
QT learning 01 GUI program principle analysis
Solr basic operation 11