迷宮實現遞歸版本C++


迷宮實現遞歸版本C++

問題描述:

////////////////////////////////////////////////////////////
//題目:迷宮求解問題。

 

大致思路:

//1、入口,出口判斷/程序終止判定:4個方位的坐標邊界比較,表明到了出入口。
//2-1、求解原理1:暴力處理,從入口點開始,對其四個方向進行可行性判別,獲取下一位置,重復,知道走到出口。
//2-2、求解原理2:對於有出口的迷宮,如果你一直靠右,或者靠左行走,必然能夠走到出口。這個方案省去了1中暴力隊每個方向的判別。
//3、走過的路線,具體坐標的值修改為2,然后將走過的點坐標入棧保存。
//4、優化點①:可能存在死胡同或者環形路線,那么必然會繞遠路。所以對3、中的修改值為2改進為+=2,可以具體得出經過某位置幾次
//5、優化點②:根據3、 4、可以判斷出走過的沒必要的路線,記錄值為4的點,然后對棧進行出棧操作,可以做到優化部分路線。
//6、待完善點:沒有給出迷宮最短路線的解答。
////////////////////////////////////////////////////////////

//工程目錄下 MazeMap.txt中的地圖表示

//1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
//1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
//1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
//1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
//1 1 0 0 0 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
//1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 1 1
//1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
//1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 1 1
//1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
//1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
//1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1
//1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1
//1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1
//1 1 0 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1 1
//1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1
//1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1 1
//1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1
//1 1 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1 1
//1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

 

 

代碼示例:

//由文件讀取迷宮地圖。
void
GetMazeMap(int *a,int row,int col) { FILE *FOut; fopen_s(&FOut,"MazeMap.txt", "r"); assert(FOut); for (int i = 0; i < row; ++i) { for (int j = 0; j < col;) { char ch = fgetc(FOut); if (ch == '0' || ch == '1') { a[i*col + j] = ch - '0'; ++j; } } } }
//迷宮打印
void PrintMazeMap(int *a, int row, int col) { cout << "MazeMap:(row,col):(" << row <<","<< col <<")."<< endl; for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { cout << a[i*col + j] << " "; } cout << endl; } cout << endl; }

//位置坐標類。
struct Step { int row;// int col;// bool operator==(Step &s) { return row == s.row&&col == s.col; } };

//在程序中對迷宮進行遍歷時除了坐標,更給出具體方向的類man
struct man { man(Step s, int d) :_cur(s) , _dir(d) {} Step _cur; int _dir;// 0-3 表明4個方向 man nextPos(int dir) { Step cur = _cur; dir = (dir+4) % 4; switch (dir) { case 0: cur.row--; break; case 1: cur.col++; break; case 2: cur.row++; break; case 3: cur.col--; break; } return man(cur, dir); } }; stack<man> paths;
//打印具體走過的迷宮路線的坐標(含方向)
void PrintPathStep() { while (!paths.empty()) { man tmp = paths.top(); cout << "[" << tmp._cur.row << "," << tmp._cur.col << "]:" << tmp._dir << "" << "-->"; paths.pop(); } cout << "Over!" << endl; } //給定map和入口點坐標,求迷宮解 void GetMazePaths(int *map, int row, int col, Step& entry) { //當前位置. man m(entry, 0); map[m._cur.row*col + m._cur.col] = 2; paths.push(m); while (1) { man top = paths.top(); man cur = top.nextPos(top._dir - 1); //man cur = top.nextPos(top._dir + 1); 可替換上行,轉換為靠右行。

if (cur._cur.col<0 || cur._cur.row<0 || cur._cur.col>=col || cur._cur.row>=row) { cout << "越界" << endl; top._dir++; //top._dir--; 可替換上行,轉換為靠右行 paths.pop(); paths.push(top); continue; } //邊界,也就是出入口 if ((cur._cur.col == 0 || cur._cur.row == 0 || cur._cur.col == col-1 || cur._cur.row == row-1) &&map[cur._cur.row*col + cur._cur.col] == 0) { cout << "這里是出入口" << endl; if (!(cur._cur == entry)) { map[cur._cur.row*col + cur._cur.col] = 2; paths.push(cur); cout << "得到出口" <<cur._cur.row<<" "<<cur._cur.col<< endl; break; } } //遍歷: //下一個位置為當前方向的下一個位置 if (map[cur._cur.row*col + cur._cur.col] != 1) { map[cur._cur.row*col + cur._cur.col] += 2; if (map[cur._cur.row*col + cur._cur.col] == 4) { Step tmp; tmp.row = cur._cur.row; tmp.col = cur._cur.col; ////////////////////////////////////////////////////////////////////////////// //回退過程。 while (paths.top()._cur.row != tmp.row || paths.top()._cur.col != tmp.col) { map[paths.top()._cur.row*col + paths.top()._cur.col] = 1; paths.pop(); } } map[cur._cur.row*col + cur._cur.col] = 2; paths.push(cur); } else { top._dir++; //top._dir--; 可替換上行,轉變為靠右行方案 paths.pop(); paths.push(top); } } }

//遞歸方式求解迷宮路線問題。思路為靠左行方案
//注:遞歸方式沒有如非遞歸方式的死胡同排除等優化,只是單純的靠左行得到路線。
void GetNextAccessPath(int *map, int row, int col, man& entry) { man tmp(entry); if ( ( entry._cur.row == row - 1 || entry._cur.col == col - 1 ||entry._cur.row == 0 // || entry._cur.col == 0 //注,這一行的注釋主要是明確出口的具體位置不是左邊。 可以改進為結束判斷是:該點是邊界,但不是程序傳入的迷宮入口。(即是出口)
) && map[entry._cur.row*col + entry ._cur.col] != 1 ) { paths.push(entry); return; } else { paths.push(entry); tmp = entry.nextPos(entry._dir-1);
    //獲得下一個可以通行的位置
while (map[tmp._cur.row*col + tmp._cur.col] == 1) { tmp = entry.nextPos(tmp._dir+1); } entry = tmp; GetNextAccessPath(map, row, col, entry); } } void main() { int a[20][20] = {}; ::GetMazeMap((int*)a, 20, 20); Step ent = { 2, 0 }; man entm = { { 2, 0 }, 1 }; GetMazePaths((int*)a, 20, 20, ent); //非遞歸方式 //GetNextAccessPath((int*)a, 10, 10, man(ent, 0)); //遞歸方式 ::PrintMazeMap((int*)a, 20, 20); ::PrintPathStep(); system("pause"); }

 

 

運行結果實例:

得到出口19 2
MazeMap:(row,col):(20,20).
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1
1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 0 0 1 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 2 1 1
1 1 0 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 1 1
1 1 0 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 2 2 2 0 2 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 2 1 2 0 2 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 2 1 2 2 2 1 0 0 0 1 1 1 1 1 1 1 1 1
1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

[19,2]:2--> [18,2]:3--> [17,2]:3--> [16,2]:3--> [15,2]:3--> [14,2]:3--> [14,3]:4--> [14,4]:0--> [15,4]:1--> [16,4]:1--> [17,4]:1--> [18,4]:5--> [18,5]:4--> [18,6]:4--> [17,6]:3-->
[16,6]:3--> [15,6]:3--> [14,6]:3--> [13,6]:3--> [12,6]:3--> [12,7]:4--> [12,8]:4--> [12,9]:4--> [12,10]:4--> [12,11]:4--> [12,12]:0--> [12,12]:3--> [12,13]:4--> [12,14]:4--> [12,15]:4-->
[12,16]:4--> [12,17]:4--> [11,17]:3--> [10,17]:3--> [9,17]:3--> [8,17]:3--> [7,17]:3--> [6,17]:3--> [5,17]:3--> [4,17]:3--> [3,17]:3--> [2,17]:3--> [2,16]:2--> [2,15]:2--> [2,14]:2-->
[2,13]:2--> [2,12]:2--> [2,11]:2--> [2,10]:2--> [2,9]:2--> [2,8]:2--> [2,7]:2--> [2,6]:2--> [2,5]:2--> [2,4]:2--> [2,3]:2--> [2,2]:2--> [2,1]:2--> [2,0]:2--> Over!

 

注:程序中的地圖大小可由文件中定義給出。

然后數組表示可以改進為動態分配。

具體關於二維數組做參數或如何動態申請二維數組的方案可參考:Effective-C++.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM