栈的应用----迷宫

使用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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值