C++利用循環(huán)和棧實(shí)現(xiàn)走迷宮
本文實(shí)例為大家分享了C++利用循環(huán)和棧實(shí)現(xiàn)走迷宮的具體代碼,供大家參考,具體內(nèi)容如下
要求:
1、將地圖的數(shù)組保存在文件中,從文件中讀取行列數(shù)
2.、動(dòng)態(tài)開(kāi)辟空間保存地圖
3.、運(yùn)行結(jié)束后再地圖上標(biāo)出具體的走法
說(shuō)明:
1、文件中第一行分別放置的是地圖的行數(shù)和列數(shù)
2、其中1表示墻,即路不通,0表示路,即通路
3、程序運(yùn)行結(jié)束后用2標(biāo)記走過(guò)的路徑
4、當(dāng)走到“死胡同”時(shí)用3標(biāo)記此路為死路
5、每到一個(gè)點(diǎn),按照 左 上 右 下 的順序去試探
6、沒(méi)有處理入口就是"死胡同"的極端情況
地圖文件截圖:
代碼示例:maze.h
#ifndef _MAZE_H_ #define _MAZE_H_ #include <stack> #include <fstream> // ifstream #include <iostream> #include <string> using namespace std; // 坐標(biāo)類(lèi) class Seat { public: Seat(int _x, int _y) :x(_x) ,y(_y) { } int x; int y; }; // 迷宮類(lèi) class Maze { private: int** _map; // 指向地圖的指針 int _row; // 存放地圖的行數(shù) int _col; // 存放地圖的列數(shù) public: // 構(gòu)造函數(shù) 讀取文件里的地圖 和行數(shù) Maze(const string& filePath); private: // 判斷是否是路 bool IsPass(Seat& entry); public: // 開(kāi)始走 bool PassMaze(stack<Seat>& s, Seat& entry); // 打印地圖 void PrintMap(); // 析構(gòu) 釋放空間 ~Maze(); }; #endif
maze.cpp
// 迷宮之遞歸實(shí)現(xiàn) #include "maze.h" // 構(gòu)造函數(shù) Maze::Maze(const string& filePath) { ifstream mapFile(filePath, ofstream::out); assert(mapFile); // 斷言文件是否存在 string str_row_col; //第一行 獲取行和列 string str_temp; // 臨時(shí)字符串 // 獲取行列的個(gè)數(shù) getline(mapFile,str_row_col);// 讀取第一行 str_temp = str_row_col.substr(0, str_row_col.find_first_of(','));// 取得字符串中的字串獲取行數(shù) _row = atoi(str_temp.c_str()); // atoi將字符串轉(zhuǎn)為數(shù)字 str_temp = str_row_col.substr(str_row_col.find_first_of(',')+1); // 取得字符串中的列數(shù) _col = atoi(str_temp.c_str()); // atoi將字符串轉(zhuǎn)為數(shù)字 // 分配空間 _map = new int*[_row]; for (int idx = 0; idx < _col; ++idx) { _map[idx] = new int[_col]; } // 填充地圖 int index_row = 0; // 放置地圖 二維數(shù)組的行索引 int index_col = 0; // 放置地圖 二維數(shù)組的列索引 while (!mapFile.eof()) { getline(mapFile, str_temp); char* a_line = (char*)str_temp.c_str(); // 遍歷一行 while (*a_line != '\0') { if (*a_line == '0' || *a_line == '1') { _map[index_row][index_col++] = *a_line - '0'; // 減0 是將字符'0'的 ASCII對(duì)應(yīng)的十進(jìn)制值減去 } a_line++; // 向后移動(dòng)指針 } ++index_row; index_col = 0; // 每處理完一行后將列索引置0 } mapFile.close(); } // 判斷是否是路 bool Maze::IsPass(Seat& entry) { if (entry.x < 0 || entry.y < 0 || entry.y >= _col || entry.x >= _row) { return true; } if (_map[entry.x][entry.y] == 0) { return true; } return false; } // 開(kāi)始走 void Maze::PassMaze( Seat& entry) { stack<Seat> s; if (IsPass(entry)) { s.push(entry); // 壓棧當(dāng)前位置 while (!s.empty()) // 棧不為空繼續(xù) { Seat curSeat = s.top(); // 取得棧頂存儲(chǔ)的位置 // 走到邊界 if (curSeat.x < 0 || curSeat.y < 0 || entry.y >= _col || entry.x >= _row ) { return; } _map[curSeat.x][curSeat.y] = 2; // 將走過(guò)的路標(biāo)記為2 // 往左走 Seat left(curSeat.x, curSeat.y-1); if (IsPass(left)) { s.push(left); continue; } // 往上走 Seat up(curSeat.x-1, curSeat.y); if (IsPass(up)) { s.push(up); continue; } // 往右走 Seat right(curSeat.x, curSeat.y+1); if (IsPass(right)) { s.push(right); continue; } // 往下走 Seat down(curSeat.x+1, curSeat.y); if (IsPass(down)) { s.push(down); continue; } // 運(yùn)行到此處說(shuō)明 每個(gè)方向都沒(méi)有路可走 是死路標(biāo)記為3 _map[curSeat.x][curSeat.y] = 3; // 出棧這個(gè)“死路位置” s.pop(); } } } // 打印地圖 void Maze::PrintMap() { for (int index_row = 0; index_row < _row; ++index_row) { for (int index_col = 0; index_col < _col; ++index_col) { cout << _map[index_row][index_col] <<" "; } cout <<endl; } cout <<endl; } // 析構(gòu) Maze::~Maze() { for (int idx = 0; idx < _row; ++idx ) { delete[] _map[idx]; } delete[] _map; _map = NULL; } test.cpp [cpp] view plain copy int main() { Maze m1("map.txt"); // 構(gòu)造一個(gè)迷宮對(duì)象 m1.PrintMap(); // 走之前打印 m1.PassMaze(Seat(9, 4)); //開(kāi)始走 傳遞迷宮入口點(diǎn) m1.PrintMap(); // 結(jié)束后再次打印 return 0; }
截圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C語(yǔ)言實(shí)現(xiàn)的順序表功能完整實(shí)例
欄 目:C語(yǔ)言
下一篇:C語(yǔ)言實(shí)現(xiàn)的雙鏈表功能完整示例
本文標(biāo)題:C++利用循環(huán)和棧實(shí)現(xiàn)走迷宮
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/810.html
您可能感興趣的文章
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 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ǔ)方式詳解
- 01-10深入理解C/C++混合編程


閱讀排行
- 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ī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子