迷宮問題——堆棧應用(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.結果:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章