這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在C++中利用遞歸走迷宮,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、安岳ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的安岳網(wǎng)站制作公司
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 <fstream> // ifstream #include <iostream> #include <cassert> #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( Seat& entry); // 打印地圖 void PrintMap(); // 析構(gòu) 釋放空間 ~Maze(); }; #endif
maze.cpp
// 遞歸實(shí)現(xiàn)迷宮 #include "maze.h" // 判斷是否是路 bool Maze::IsPass( Seat& Entry) { if (_map[Entry.x][Entry.y] == 0) { return true; } return false; } // 構(gòu)造函數(shù) 讀取文件里的地圖 和行數(shù) Maze::Maze(const string& filePath ) { ifstream read(filePath); string str_row_col, str_temp; // 讀取第一行 getline(read, str_row_col); // 獲取行 str_temp = str_row_col.substr(0, str_row_col.find_first_of(',')); _row = atoi(str_temp.c_str()); // 獲取列 str_temp = str_row_col.substr( str_row_col.find_first_of(',')+1); _col = atoi(str_temp.c_str()); // 為地圖分配空間 _map = new int*[_row]; for (int index = 0; index < _col; ++index) { _map[index] = new int[_col]; } int index_col = 0; int index_row = 0; // 填充地圖 while (!read.eof()) { // 獲取一行 getline(read, str_temp); char * line = (char*)str_temp.c_str(); while ((*line) != '\0') { if (*line == '0' || *line == '1') { _map[index_row][index_col++] = *line - '0'; } ++line; } ++index_row; index_col = 0; } // 關(guān)閉文件 read.close(); } // 開(kāi)始走 bool Maze::PassMaze( Seat& Entry) { // 判斷是否走到出口 if (Entry.x < 0 || Entry.y < 0 || Entry.y >=_row) { return true; } if (IsPass(Entry)) { // 將走過(guò)的路置為2 _map[Entry.x][Entry.y] = 2; // 向左走 Seat left(Entry.x, Entry.y-1); if (PassMaze(left))// 遞歸調(diào)用 { return true; } // 向上走 Seat up(Entry.x-1, Entry.y); if (PassMaze(up)) // 遞歸調(diào)用 { return true; } // 向右走 Seat right(Entry.x, Entry.y+1); if (PassMaze(right)) // 遞歸調(diào)用 { return true; } // 向下走 Seat down(Entry.x+1, Entry.y); if (PassMaze(down)) { return true; } // 走到此處說(shuō)明是死路 置為3 _map[Entry.x][Entry.y] = 3; } return false; } // 打印地圖 void Maze::PrintMap() { for (int row = 0; row<_row; ++row) { for (int col = 0; col<_col; ++col) { cout << _map[row][col] << " "; } cout <<endl; } cout <<endl; } // 釋放空間 Maze::~Maze() { for (int idx = 0; idx < _row; ++idx ) { delete[] _map[idx]; } delete[] _map; _map = NULL; }
上述就是小編為大家分享的怎么在C++中利用遞歸走迷宮了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:怎么在C++中利用遞歸走迷宮
本文鏈接:http://muchs.cn/article46/pphohg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、外貿(mào)建站、App設(shè)計(jì)、小程序開(kāi)發(fā)、定制網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)