定義乙個二維陣列:
int maze[5][5]= ;
它表示乙個迷宮,其中的1表示牆壁,0表示可以走的路,只能橫著走或豎著走,不能斜著走,要求程式設計序找出從左上角到右下角的最短路線。
input
乙個5 × 5的二維陣列,表示乙個迷宮。資料保證有唯一解。
output
左上角到右下角的最短路徑,格式如樣例所示。
sample input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
sample output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
思路:通過bfs得到到達終點的每個途徑,使用結構體陣列記錄每個途徑的完整走法,
在得到最短途徑後,抽出整條途徑,並回朔重現
**:
#include #include #include #include using namespace std;
typedef pairp;
p st,en;
struct proc
;const int n=10;
int maze[n][n];
int n;
bool vis[n][n];
int ar[4][2]=,,,};
queuer;
void bfs()
if(temp.x==en.first && temp.y==en.second)}}
}}int main()
printf("(0, 0)\n");
while(pr.size())
return 0;
}
迷宮問題bfs
迷宮問題 採用佇列的廣度優先遍歷 bfs 思想是從乙個頂點v0開始,輻射狀地優先遍歷其周圍較廣的區域 找到的解為最優解 include define m 8 define n 8 define maxsize 1000 typedef struct box typedef struct qutype...
迷宮問題BFS
the code 資料結構迷宮.cpp 定義控制台應用程式的入口點。include stdafx.h include include include include define n 4 定義迷宮為4 4 using namespace std struct pot 為記錄路徑的rec準備,座標 x...
迷宮問題bfs
小明置身於乙個迷宮,請你幫小明找出從起點到終點的最短路程。小明只能向上下左右四個方向移動。輸入包含多組測試資料。輸入的第一行是乙個整數t,表示有t組測試資料。每組輸入的第一行是兩個整數n和m 1 n,m 100 接下來n行,每行輸入m個字元,每個字元表示迷宮中的乙個小方格。字元的含義如下 s 起點 ...