迷宫问题用递归求全部解和用栈求一个解(C++)

SqStack.h文件

#include <iostream>
#include <assert.h>
#include <iomanip>
#include <fstream>
using namespace std;

template<typename T>class SqStack
{
private:
	T *base,*top;
	int stacksize;
public:
	SqStack(int k=1)
	{
		base = new T[k];
		assert(base != NULL);
		top = base;
		stacksize = k;
	}
	~SqStack()
	{
		delete []base;
	}
	void ClearStack()
	{
		top = base;
	}
	bool StackEmpty()const
	{
		return top == base;
	}
	int StackLength()const
	{
		return top-base;
	}
	bool GetTop(T &e)const
	{
		if(top > base)//栈不空
		{
			e = *(top - 1);
			return true;
		}
		else
			return false;
	}
	bool Push(T e)
	{
		T *newbase;
		if (top - base == stacksize)
		{
			newbase = new T[stacksize*2];
			if(newbase == NULL)
				return false;
			for (int j=0; j<top-base; j++)
				*(newbase+j) = *(base+j);
			delete[]base;
			base = newbase;
			top = base+stacksize;
			stacksize*=2;
		}
		*top++=e;
		return true;
	}
	bool Pop(T &e)
	{	
		if(top == base)
			return false;
		e=*--top;
		return true;
	}
	void StackTraverse()const
	{
		T *p=base;
		while(p < top)
		{
			cout<<*p<<" ";
			p++;
		}
		cout<<endl;
	}
};


PosType.h文件

struct PosType //二维坐标位置类型
{
	int x,y;
};
PosType operator+(const PosType p1,const PosType p2)
{
	PosType p;
	p.x = p1.x+p2.x;
	p.y = p1.y+p2.y;
	return p;
}
bool operator==(const PosType p1,const PosType p2)
{
	return p1.x==p2.x && p1.y==p2.y;
}

struct PosDirect
{
	PosType pos;
	int direct;
};
Alog.cpp文件
#include "SqStack.h"
#include "PosType.h"
const int MAXLENGTH = 10;
class MAZE
{
private:
	int maze[MAXLENGTH][MAXLENGTH];
	int row,col;
	int count;
	PosType begin,end;
	static PosType direc[4];
	void Try(PosType curpos,int curstep)
	{
		PosType nextpos;
		for (int i=0; i<=3; i++)
		{
			nextpos = curpos + direc[i];
			if (maze[nextpos.x][nextpos.y]==-1)
			{
				maze[nextpos.x][nextpos.y] = ++curstep;
				if (nextpos==end)
				{
					cout<<"解"<<++count<<endl;
					Print();
				}
				else
					Try(nextpos,curstep);
				maze[nextpos.x][nextpos.y] = -1;
				curstep--;
			}
		}
	}
	void Print()const
	{
		for(int i=0; i<row; i++)
		{
			for (int j=0; j<col; j++)
			{
				if( maze[i][j] == 0)
					cout<<"█";
				else if( maze[i][j] < 0)
					cout<<"  "; 
				else
					cout<<setw(2)<<maze[i][j];
			}
				//cout<<;
			cout<<endl;
		}
	}
public:
	MAZE(char *FileName)
	{
		ifstream fin(FileName);
		fin>>row>>col;
		for (int i=0; i<row; i++)
		{
			for(int j=0; j<col; j++)
				fin>>maze[i][j];
		}
		cout<<"迷宫结构如下:\n";
		Print();
		fin>>begin.x>>begin.y;
		fin>>end.x>>end.y;
		cout<<"迷宫起点:("<<begin.x<<","<<begin.y<<")"<<endl;
		cout<<"迷宫终点:("<<end.x<<","<<end.y<<")"<<endl;
		fin.close();
	}
	bool MazePathByStack()
	{
		SqStack<PosDirect> S(8);
		PosDirect e;
		PosType curpos = begin;
		int curstep = 1;
		do 
		{
			if (maze[curpos.x][curpos.y] == -1)
			{
				maze[curpos.x][curpos.y] = curstep;
				e.pos = curpos;
				e.direct = 0;
				S.Push(e);
				curstep++;
				if (curpos == end)
				{
					cout<<"此迷宫从入口到出口的一条路径如下:\n";
					Print();
					return true;
				}
				else
					curpos = curpos + direc[e.direct];
			}
			else
			{
				if( !S.StackEmpty() )
				{
					S.Pop(e);
					curstep--;
					while ( e.direct==3 && !S.StackEmpty() )
					{
						maze[e.pos.x][e.pos.y] = -2;
						S.Pop(e);
						curstep--;
					}
					if ( e.direct <3 )
					{
						e.direct++;
						S.Push(e);
						curstep++;
						curpos = e.pos + direc[e.direct];
					}
				}
			}
		} while (!S.StackEmpty());
		cout<<"此迷宫没有从入口到出口的路径\n";
		return false;
	}
	void MazePathByRecursion()
	{
		maze[begin.x][begin.y] = 1;
		count = 0;
		Try(begin,1);
	}
};
PosType MAZE::direc[4] = { {1,0},{0,1},{0,-1},{-1,0} };


void main()
{
	int i;
	MAZE M("F2-1.txt");
	cout<<"用递归方法求迷宫所有解请输入1;用栈方法求迷宫的一个解请输入0:";
	cin>>i;
	if ( i == 0 )
		M.MazePathByStack();
	else
		M.MazePathByRecursion();
	
	system("pause");
}


F2-1.txt

10 10
 0  0  0  0  0  0  0  0  0  0
 0 -1 -1  0 -1 -1 -1  0 -1  0
 0 -1  0  0 -1 -1 -1  0 -1  0
 0 -1 -1 -1 -1  0  0 -1 -1  0
 0 -1  0  0  0 -1 -1 -1 -1  0
 0 -1 -1 -1  0  0 -1 -1 -1  0
 0 -1  0 -1 -1 -1  0 -1 -1  0
 0 -1  0 -1  0 -1  0  0 -1  0
 0  0 -1 -1 -1 -1 -1 -1 -1  0
 0  0  0  0  0  0  0  0  0  0
1 1
8 8


运行效果:

用栈求一个解




递归求全部解




corlymeng


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值