使用C++构建带环回路迷宫,并使用迭代法,递归法求出口
使用递归方法求迷宫的最短路径(其实在打印最短路径是使用队列的话路径就不是反着的了)
#pragma once
#include<iostream>
using namespace std;
#include<stack>
#include<vector>
typedef struct Pos {
size_t _row;
size_t _col;
}Pos;
class Solution {
public:
//初始化迷宫
Solution(vector<vector<int>> maze,Pos path)
:_maze(maze){}
//判断next是否可走
bool MazeCheckIsAccess(Pos cur) {
if (cur._col >= 0 && cur._col < _maze[0].size()
&& cur._row >= 0 && cur._row < _maze.size()
&& _maze[cur._row][cur._col] == 1) {
return true;
}
else {
return false;
}
}
//迭代法走迷宫
bool MazeGetPath(Pos path) {
stack<Pos> s;
s.push(path);
Pos cur = path;
Pos next = cur;
while (!s.empty()) {
cur = s.top();
next = cur;
_maze[cur._row][cur._col] = 2;
if (next._col == _maze[0].size() - 1) {
return true;
}
next = cur;
next._col -= 1;
if (MazeCheckIsAccess(next)) {
s.push(next);
continue;
}
next = cur;
next._col += 1;
if (MazeCheckIsAccess(next)) {
s.push(next);
continue;
}
next = cur;
next._row -= 1;
if (MazeCheckIsAccess(next)) {
s.push(next);
continue;
}
next = cur;
next._row += 1;
if (MazeCheckIsAccess(next)) {
s.push(next);
continue;
}
s.pop();
}
return false;
}
//递归法走迷宫
void MazeGetPathR(Pos path) {
Pos next = path;
_maze[next._row][next._col] = 2;
if (next._col == _maze[0].size() - 1) {
return;
}
next = path;
next._col -= 1;
if (MazeCheckIsAccess(next)) {
MazeGetPathR(next);
}
next = path;
next._col += 1;
if (MazeCheckIsAccess(next)) {
MazeGetPathR(next);
}
next = path;
next._row -= 1;
if (MazeCheckIsAccess(next)) {
MazeGetPathR(next);
}
next = path;
next._row += 1;
if (MazeCheckIsAccess(next)) {
MazeGetPathR(next);
}
}
//在求最短路径时判断next是否可走
bool MazeCheckIsAccess(Pos next,Pos cur) {
if (next._col >= 0 && next._col < _maze[0].size()
&& next._row >= 0 && next._row < _maze.size()
&& (_maze[next._row][next._col] == 1
|| _maze[next._row][next._col] > _maze[cur._row][cur._col])) {
return true;
}
else {
return false;
}
}
//求最短路径
void MazeGetShortPath(Pos entry, stack<Pos>& path) {
Pos next = entry;
if (path.empty()) {
_maze[next._row][next._col] = 2;
}
else {
Pos prev = path.top();
_maze[next._row][next._col] = _maze[prev._row][prev._col] + 1;
}
path.push(entry);
if (next._col == _maze[0].size() - 1) {
ShortPath = path;
return;
}
next = entry;
next._col -= 1;
if (MazeCheckIsAccess(next, entry)) {
MazeGetShortPath(next, path);
}
next = entry;
next._col += 1;
if (MazeCheckIsAccess(next, entry)) {
MazeGetShortPath(next, path);
}
next = entry;
next._row -= 1;
if (MazeCheckIsAccess(next, entry)) {
MazeGetShortPath(next, path);
}
next = entry;
next._row += 1;
if (MazeCheckIsAccess(next, entry)) {
MazeGetShortPath(next, path);
}
path.pop();
}
//打印迷宫
void MazePrint() {
for (size_t i = 0; i < _maze.size(); ++i) {
for (size_t j = 0; j < _maze[i].size(); ++j) {
cout << _maze[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
//打印最短路径
void MazeShortPrint() {
while (!ShortPath.empty()) {
printf("[%d,%d] <- ", ShortPath.top()._row, ShortPath.top()._col);
ShortPath.pop();
}
cout << "Begin" << endl;
}
public:
vector<vector<int>> _maze;
stack<Pos> ShortPath;
};
void Test() {
vector<vector<int>> maze =
{
{ 0,0,0,0,0,0 },
{ 0,0,1,1,1,0 },
{ 0,0,1,0,1,0 },
{ 0,0,1,1,1,0 },
{ 0,0,1,0,1,1 },
{ 0,0,1,0,0,0 },
};
Solution s(maze, { 5,2 });
s.MazePrint();
//s.MazeGetPath();
//s.MazeGetPathR({ 5,2 });
stack<Pos> st;
s.MazeGetShortPath({ 5,2 },st);
s.MazePrint();
s.MazeShortPrint();
}