日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 3984 迷宫问题

發布時間:2023/12/4 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3984 迷宫问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

定義一個二維數組:

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)

解題思路:
用一個二維數組記錄路徑。

代碼如下:

#include <iostream> #include <queue> #include <cstdio> using namespace std; const int N = 110; char g[N][N]; bool vis[N][N];int dx[] = {0, 0, 1, -1};int dy[] = {1, -1, 0, 0};struct node {int x, y;int step;int path[105][3]; };void bfs() {queue<node>q;int sx = 0, sy = 0;int ex = 4, ey = 4;node start;start.x = sx;start.y = sy;start.step = 0;start.path[0][0] = sx;start.path[0][1] = sy;vis[0][0] = true;q.push(start);while (q.size()) {node t = q.front();q.pop();if (t.x == ex && t.y == ey) {for (int i = 0; i < t.step; i++) {printf("(%d, %d)\n", t.path[i][0], t.path[i][1]);}cout << "(4, 4)" << endl;}for (int i = 0; i < 4; i++) {int xx = t.x + dx[i];int yy = t.y + dy[i];if (xx < 0 || xx > 4 || yy < 0 || yy > 4)continue;if (vis[xx][yy] || g[xx][yy] == '1')continue;node next;next = t;next.x = xx;next.y = yy;next.step = t.step + 1;next.path[next.step][0] = xx;next.path[next.step][1] = yy;vis[xx][yy] = true;q.push(next);}} }int main() {for (int i = 0; i < 5; i++)for (int j = 0; j < 5; j++)cin >> g[i][j];bfs();return 0; }

總結

以上是生活随笔為你收集整理的POJ 3984 迷宫问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。