迷宫问题——堆栈应用(C++版)

1.设计一个坐标类:

class mazepoint
{
	friend class mazestack;
public:
	mazepoint(){}
	mazepoint(int a, int b)
	{
		x = a;
		y = b;
	}
	~mazepoint(){}
private:
	int x;
	int y;
	mazepoint* next;
};

2.设计一个堆栈,存储坐标点:

//定义一个栈,可以将坐标放进栈中
class mazestack
{
public:
	mazestack()
	{
		length = 0;
		top = NULL;
	}
	~mazestack(){}
	void push(const mazepoint* item)
	{
		if (top == NULL)
		{
			mazepoint* newnode = new mazepoint;
			newnode->x = item->x;
			newnode->y = item->y;
			newnode->next = NULL;
			top = newnode;
			++length;
		}
		else
		{
			mazepoint* newnode = new mazepoint;
			newnode->x = item->x;
			newnode->y = item->y;
			newnode->next = top;
			top = newnode;
			++length;
		}
	}
	void pop()
	{
		if (top == NULL)
		{
			cout << "栈为空" << endl;
			exit(1);
		}
		else
		{
			mazepoint* temp = top;
			top = top->next;
			delete temp;
			--length;
		}
	}
	bool empty()
	{
		return top == NULL;
	}
private:
	int length;
	mazepoint* top;
};

3.设计求解迷宫问题类:

//以后建立动态数组存储迷宫数据
class mazerun
{
public:
	mazerun()
	{
		m = 7;
		int (*maze)[7] = new int[7][7];
	}
	~mazerun(){}
	
	void createmaze() //产生一个m*m的随机整数迷宫
	{
		//将迷宫最外层添加一层墙壁,值为1
		for (int i = 0; i < m ; ++i)
		{
			maze[i][0] = maze[i][m-1] = 1;
			maze[0][i] = maze[m-1][i] = 1;
		}

		//用随机数填充迷宫:0表示可通路,1表示障碍,2表示已经走过路,3表示可通路径不正确点
		cout << "请输入迷宫m-1*m-1,并使迷宫存在一条通路" << endl;
	//	cout << "默认情况下:第一个点和最后一个点为进,出口,值为0" << endl;
		int num=0;
		int cc = 0;
		vector<int> vect;
		cout << "输入迷宫数" << endl;
		while (cin >> num)
		{
			vect.push_back(num);
		}
		for (int i = 1; i < m - 1; ++i)
		{
			for (int j = 1; j < m - 1; ++j)
			{
			    maze[i][j] = vect[cc++];
		    }

		}
				
		maze[1][1] = 0; //入口
		maze[m-2][m-2] = 0; //出口
		//输出完整的迷宫 

		cout << "完整的迷宫:" << endl;
		for (int i = 0; i < m; ++i)
		{
			for (int j = 0; j < m; ++j)
				cout << maze[i][j]<<" ";
			cout << endl;
		}

	}

	bool findpath()
	{
		mazepoint* start = new mazepoint(1, 1); //起点
	//	mazepoint* end = new mazepoint(m - 2, m - 2);//终点
		mazestack mazepath;
		mazepath.push(start);//将起始点压入路径栈中
		bool flag = 1; //当找到出口时置为0
		int x = 1, y = 1;
		maze[1][1] = 1; //阻止返回入口
		//从上下左右四个方向找出可通过的路径
		while (flag)   //当找不到出口时,循环结束
		{
			if (maze[x - 1][y] == 0)
			{
				maze[x - 1][y] = 2;
				mazepath.push((new mazepoint(x - 1, y)));
				//设置当前点为新起点
				x = x - 1;
				y = y;
			}
			else if (maze[x +1][y] == 0)
			{
				maze[x +1][y] = 2;
				mazepath.push((new mazepoint(x + 1, y)));
				//设置当前点为新起点
				x = x + 1;
				y = y;
			}
			else if (maze[x ][y-1] == 0)
			{
				maze[x ][y-1] = 2;
				mazepath.push((new mazepoint(x , y-1)));
				//flag = 1;
				//设置当前点为新起点
				x = x;
				y = y - 1;
			}
			else if (maze[x ][y+1] == 0)
			{
				maze[x ][y+1] = 2;
				mazepath.push((new mazepoint(x , y+1)));
			//	flag = 1;
				//设置当前点为新起点
				x = x;
				y = y + 1;
			}

			//判断是否走到出口,是否将出口设为起点
			if ((x == m - 2) && (y == m - 2))
			{
				cout << "迷宫出口已找到" << endl;
				flag = 0;
				return 1;
			}

	
			if ((maze[x - 1][y] != 0) && (maze[x + 1][y] != 0) && (maze[x][y - 1] != 0) && (maze[x][y + 1] != 0)) 
			{
				if (maze[x - 1][y] == 2)
				{
					maze[x][y] = 3;
					mazepath.pop();
				//	flag = 1;
					//设置当前点为新起点
					
					x = x - 1;
					y = y;
				}
				else if (maze[x + 1][y] == 2)
				{
					maze[x][y] = 3;
					mazepath.pop();
					//flag = 1;
					//设置当前点为新起点
				
					x = x + 1;
					y = y;
				}
				else if (maze[x][y - 1] == 2)
				{
					maze[x][y] = 3;
					mazepath.pop();
					//flag = 1;
					//设置当前点为新起点
				
					x = x;
					y = y - 1;
				}
				else if (maze[x][y + 1] ==2)
				{
					maze[x][y] = 3;
					mazepath.pop();
					//flag = 1;
					//设置当前点为新起点
					
					x = x;
					y = y + 1;
				}
				else if ((maze[x - 1][y] != 2) && (maze[x + 1][y] != 2) && (maze[x][y - 1] != 2) && (maze[x][y + 1] != 2))
				{
					cout << "迷宫无路可走" << endl;
					flag = 0;
					return 0;
				}


			}
			
		}

	}

	void printpath()
	{
		if (findpath())
		{
			maze[1][1] = 2;
			for (int i = 0; i < m; i++)
			{
				for (int j = 0; j < m; j++)
				{
					if (maze[i][j] == 2)
						cout << '*' << "  ";
					else if (maze[i][j] == 3)
						cout << 0 << "  ";
					else
						cout << 1 << "  ";
				}
				cout << endl;
			}
		}
		else
		{
			cout << "迷宫没有通路,不输出路径" << endl;
		}

	}

private:
 int m;
 int maze[10][10];
};

PS: 以上是定义在头文件maze.h中。

4.主函数

#include<iostream>
#include"maze.h>

int main()
{
   mazerun run;
   run.createmaze();
   run.printpath();
   system("pause");
   return 0;
}

5.结果:

《迷宫问题——堆栈应用(C++版)》《迷宫问题——堆栈应用(C++版)》

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/wf131410000/article/details/46461567
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞