1. 程式人生 > >迷宮問題(BFS+保存路徑) POJ No.3984

迷宮問題(BFS+保存路徑) POJ No.3984

布爾 路線 namespace 二維數組 con 保存 tin 分析 code

Description

定義一個二維數組:

int maze[5][5] = {
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,
};

它表示一個迷宮,其中的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)
分析:
因為上一篇中的迷宮問題只需要輸出最小步數一個整數,所以可以將用來表示訪問過的狀態數組和遞增步數的數組合
為一個整型二維數組。
而這裏需要記錄的是路徑,即每一步走過的坐標,所以可以另設一個自定義的數組用來存坐標,另設一個布爾類型數組表示訪問狀態。在後
面遍歷記錄路徑的數組時發現仍然需要最小步數來做判斷條件,所以自定義了一個含x,y,z的類,z用來計算最小步數。
關於保存路徑:
將已經訪問過的狀態用標記管理起來,就可以實現由近及遠的搜索,從終點往回遍歷foot一定是最短路徑。
你可以找一條比最短路徑長的路徑,然後分析路徑上的特殊狀態會發現,因為被訪問過的狀態不能再被訪問,所以最長路徑上的有些狀態已
經被短路徑訪問過,所以這條長路徑會在這中斷而不會到達終點,所以將已經訪問過的狀態用標記管理起來,就可以實現由近及遠的搜索。

代碼如下:
#include <iostream>
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<cstring>
    #include<string>
    #include<vector>
    #include
<set> #define INF 0x3f3f3f3f #define MAX 10 using namespace std; struct node{ int x, y, z; }; int mp[MAX][MAX]; node foot[MAX][MAX]; int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; bool visit[MAX][MAX]; int bfs(){ queue<node> que; que.push((node){ 0,0,0 }); while(!que.empty()){ node p = que.front(); que.pop(); if(p.x == 4 && p.y == 4) return p.z; if(visit[p.x][p.y]) continue; for(int i = 0; i < 4; i++){ node n; n.x = p.x + dx[i]; n.y = p.y + dy[i]; n.z = p.z + 1; if(0 <= n.x && n.x < 5 && 0 <= n.y && n.y < 5){ if(!visit[n.x][n.y] && !mp[n.x][n.y]){ que.push(n); foot[n.x][n.y].x = p.x; foot[n.x][n.y].y = p.y; } } } } return 0; } int main(){ memset(visit, false, sizeof(visit)); for(int i = 0; i < 5; i++) for(int j = 0; j < 5; j++){ scanf("%d", &mp[i][j]); } int len = bfs(); int a = 4, b = 4; node arr[MAX]; for(int i = 0; i < len; i++){ arr[i].x = foot[a][b].x; arr[i].y = foot[a][b].y; a = arr[i].x; b = arr[i].y; } for(int i = len - 1; i >= 0; i--){ printf("(%d, %d)\n", arr[i].x, arr[i].y); } printf("(4, 4)\n"); return 0; }

 

迷宮問題(BFS+保存路徑) POJ No.3984