迷宫问题,二维数组模拟迷宫;

这个程序使用顺序栈来解决二维数组模拟的迷宫问题,通过判断上下左右四个方向是否可以通过,寻找从起点到终点的路径。如果迷宫无通路,程序会输出相应提示。用户可以输入迷宫的宽度和高度,然后逐行输入迷宫的数据。程序会显示迷宫模型并尝试找到通路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// Datastructure2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"

using namespace std;
static int w;
static int h;
static int column = 0;
static int row = 0;
static int **A;		//声明二级指针A,指向一个指针数组

//***************************************************************************
//顺序栈实现
//***************************************************************************
template<class T>
class ArrayStack
{
private:
	int size;//栈的大小
	int tos;//栈顶索引
	T*contain;
public:
	ArrayStack():size(0),tos(-1),contain(NULL){}
	ArrayStack(int maxsize);
	~ArrayStack();
	void Push(const T&element);
	T& GetTop();
	T&Pop();
	bool IsEmpty();
	void MakeEmpty();
};
template<class T>
ArrayStack<T>::ArrayStack(int maxsize)
{
	size=maxsize;
	tos=-1;
	contain=new T[maxsize];
}
template<class T>
ArrayStack<T>::~ArrayStack()
{
	delete contain;
	tos=-1;
	size=0;
}
template<class T>
void ArrayStack<T>::Push(const T&element)
{
	if(tos==size-1)
	{
		//cout<<"栈已经满"<<endl;
		return;
	}
	else
	{
		tos++;
		contain[tos]=element;
	}
}
template<class T>
T& ArrayStack<T>::GetTop()
{
	return contain[tos];
}
template<class T>
T& ArrayStack<T>::Pop()
{
	if(tos==-1)
	{
		int i=0;
		//cout<<"栈为空"<<endl;
		return contain[tos--];
	}
	else
	{
		return contain[tos--];
	}
}
template<class T>
bool ArrayStack<T>::IsEmpty()
{
	if(tos==-1)
		return true;
	else
		return false;
}
template<class T>
void ArrayStack<T>::MakeEmpty()
{
	delete contain;
	tos=-1;
	size=0;
}

bool Right(int column,int row){
	if (column + 1 < w) {
			if (A[column + 1][row] == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
}

bool Left(int column,int row){
	if (column - 1 >= 0) {
			if (A[column - 1][row] == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
}

bool Down(int column,int row){
	if (row + 1 < h) {
			if (A[column][row + 1] == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
}

bool Up(int column,int row){
	if (row - 1 >= 0) {
			if (A[column][row - 1] == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
}

bool isPassway(){
	ArrayStack<int>*point=new ArrayStack<int>(w*h);
	point->Push(col
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值