迷宮求解
一:迷宮求解是一個鍛鍊我們的算法求解能力的問題,它的實現方法有很多;今天我們就介紹其中的用棧求解的方法。
二:什麼是棧:
大家應該都有往袋子裏裝東西的經歷,在往袋子裏裝滿東西之後,當我們去取的時候,總是先從最後放進去的東西的地方去取。也就是後進先出(FILO)。雖然棧的單向性用起來會沒有鏈表那樣可以在任意位置對數據進行操作,但是正因爲如此棧也帶來了很大的方便。
三:迷宮求解
現在我們要在下面的迷宮尋找一條可行的路徑
1 1 1 1 1 1 1 1 1 1
1 0 0 1 1 0 0 0 0 1
1 0 0 0 1 0 1 0 0 1
1 0 1 0 0 0 1 1 0 1
1 0 1 0 0 0 1 1 1 1
1 0 1 1 1 0 1 1 1 1
1 0 1 1 1 0 1 1 1 1
1 1 1 1 1 0 0 0 1 1
1 1 1 1 1 1 1 0 0 1
1 1 1 1 1 1 1 1 1 1
首先我們需要在程序中表示上面的迷宮,該問題可以用數組實現
1:棧的定義
/************************************************************************/ /*自定義棧 */ /*通過自定義的簡單棧以滿足迷宮求解 */ /*功能:push() 將元素加入棧中 */ /* pop() 退棧;topValue() 獲得棧頂元素 */ /* clear() 清除棧 length() 獲得棧中元素個數*/ /************************************************************************/ #include <stack> #include <iostream> using namespace std; template<typename Elem> class PathStack: public stack<Elem> { private: int size; int top; Elem* listArray; public: PathStack(int sz = DefaultListSize){ size = sz; top = 0; listArray = new Elem[sz]; } ~PathStack(){ delete []listArray; } void clear(){ top = 0; } /****向棧中加入元素****/ bool push(const Elem& item); /***********退棧**********/ Elem pop(); /********獲得棧頂元素*******/ Elem topValue() const; int length() const { return top; } }; template<typename Elem> bool PathStack<typename Elem>::push(const Elem& item){ if(top == size) return false; listArray[top++] = item; return true; } template<typename Elem> Elem PathStack<typename Elem>::pop(){ Elem it; if(top == 0) return it; it = listArray[--top]; return it; } template<typename Elem> Elem PathStack<typename Elem>::topValue() const{ Elem it; if(top == 0) return it; it = listArray[top - 1]; return it; }
2:如何實現路徑的尋找
1:設定尋找的方向,可以使用一個判斷語句;判斷起始位置周圍哪個地方有路就將該位置的座標加入到棧中,並將該位置標記(將改位置值改爲2,既將走過的位置標記爲2)
2:判斷該位置周圍是否還有路,若沒有則退棧即退回到上一個位置;並將該位置做下另一個標記(將該位置值改爲3,既將退棧位置值用3標記)
3:重複1,2步驟直到達到出口
路徑尋找的類:
//迷宮求解的方法類
//功能:通過findPath() 方法實現對路徑的查找
// 通過printPath()方法將路徑打印出來
#include "PathStack.h"
#include <iostream>
using namespace std;
class MazeSolveMethod
{
private:
static int maze[10][10];//存放迷宮數據
PathStack<int> pathStack;//定義棧
public:
MazeSolveMethod():pathStack(100){
}
~MazeSolveMethod(){ }
void findPath(int startX,int startY);
void printPath() const;
};
int MazeSolveMethod::maze[10][10] = {
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,1,0,0,0,0,1},
{1,0,0,0,1,0,1,0,0,1},
{1,0,1,0,0,0,1,1,0,1},
{1,0,1,0,0,0,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1},
{1,1,1,1,1,0,0,0,1,1},
{1,1,1,1,1,1,1,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
};
void MazeSolveMethod::findPath(int startX,int startY){
int x = startX;
int y = startY;
pathStack.push(x);
pathStack.push(y);
maze[x][y] = 2;
cout<<"進入方法!"<<endl;
while(true){
if(maze[x-1][y] == 0){
pathStack.push(--x);
pathStack.push(y);
maze[x][y] = 2;
}else if(maze[x][y-1] == 0){
pathStack.push(x);
pathStack.push(--y);
maze[x][y] = 2;
}else if(maze[x][y+1] == 0){
pathStack.push(x);
pathStack.push(++y);
maze[x][y] = 2;
}else if(maze[x+1][y] == 0){
pathStack.push(++x);
pathStack.push(y);
maze[x][y] = 2;
}
if(maze[x-1][y] != 0 && maze[x][y+1] != 0 && maze[x+1][y] != 0 && maze[x][y-1] != 0){
if(x >= 8 && y >= 8){
break;
}else{
maze[x][y] = 3;
y = pathStack.pop();
x = pathStack.pop();
}
y = pathStack.topValue();
int temp = pathStack.pop();
x = pathStack.topValue();
pathStack.push(temp);
}
}
}
void MazeSolveMethod::printPath() const{
cout<<"printPath"<<endl;
for(int i=0; i<10; i++){
for(int j=0; j<10; j++){
if(maze[i][j] == 2)
cout<<'*'<<" ";
else if(maze[i][j] == 3)
cout<<0<<" ";
else
cout<<1<<" ";
}
cout<<endl;
}
}
主函數類
/************************************************************************/
/*迷宮求解----棧方法實現*/
//功能:通過對棧實現迷宮算法求解
//Author:Andrew
//Date :2012-10-20
/************************************************************************/
#include "MazeSolveMethod.h"
#include <iostream>
using std::cout;
using std::endl;
int main(){
MazeSolveMethod solve;
solve.findPath(1,1);
solve.printPath();
system("pause");
return 0;
}
將上面的代碼運行後結果截圖如下:
其中* 爲路徑