C++ 自定義棧實(shí)現(xiàn)迷宮求解
C++ 自定義棧實(shí)現(xiàn)迷宮求解
一:迷宮求解
是一個(gè)鍛煉我們的算法求解能力的問(wèn)題,它的實(shí)現(xiàn)方法有很多;今天我們就介紹其中的用棧求解的方法。
二:什么是棧:
大家應(yīng)該都有往袋子里裝東西的經(jīng)歷,在往袋子里裝滿(mǎn)東西之后,當(dāng)我們?nèi)ト〉臅r(shí)候,總是先從最后放進(jìn)去的東西的地方去取。也就是后進(jìn)先出(FILO)。雖然棧的單向性用起來(lái)會(huì)沒(méi)有鏈表那樣可以在任意位置對(duì)數(shù)據(jù)進(jìn)行操作,但是正因?yàn)槿绱藯R矌?lái)了很大的方便。
三:迷宮求解
現(xiàn)在我們要在下面的迷宮尋找一條可行的路徑
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
首先我們需要在程序中表示上面的迷宮,該問(wèn)題可以用數(shù)組實(shí)現(xiàn)
1:棧的定義
/************************************************************************/ /*自定義棧 */ /*通過(guò)自定義的簡(jiǎn)單棧以滿(mǎn)足迷宮求解 */ /*功能:push() 將元素加入棧中 */ /* pop() 退棧;topValue() 獲得棧頂元素 */ /* clear() 清除棧 length() 獲得棧中元素個(gè)數(shù)*/ /************************************************************************/ #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:如何實(shí)現(xiàn)路徑的尋找
1:設(shè)定尋找的方向,可以使用一個(gè)判斷語(yǔ)句;判斷起始位置周?chē)膫€(gè)地方有路就將該位置的坐標(biāo)加入到棧中,并將該位置標(biāo)記(將改位置值改為2,既將走過(guò)的位置標(biāo)記為2)
2:判斷該位置周?chē)欠襁€有路,若沒(méi)有則退棧即退回到上一個(gè)位置;并將該位置做下另一個(gè)標(biāo)記(將該位置值改為3,既將退棧位置值用3標(biāo)記)
3:重復(fù)1,2步驟直到達(dá)到出口
路徑尋找的類(lèi):
//迷宮求解的方法類(lèi) //功能:通過(guò)findPath() 方法實(shí)現(xiàn)對(duì)路徑的查找 // 通過(guò)printPath()方法將路徑打印出來(lái) #include "PathStack.h" #include <iostream> using namespace std; class MazeSolveMethod { private: static int maze[10][10];//存放迷宮數(shù)據(jù) 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<<"進(jìn)入方法!"<<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; } }
主函數(shù)類(lèi)
/************************************************************************/ /*迷宮求解----棧方法實(shí)現(xiàn)*/ //功能:通過(guò)對(duì)棧實(shí)現(xiàn)迷宮算法求解 //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; }
將上面的代碼運(yùn)行后結(jié)果截圖如下:
其中* 為路徑
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
欄 目:C語(yǔ)言
下一篇:C++中的菱形繼承深入分析
本文標(biāo)題:C++ 自定義棧實(shí)現(xiàn)迷宮求解
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1399.html
您可能感興趣的文章
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10深入理解C++中常見(jiàn)的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類(lèi)算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)方式詳解


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改