題目描述
給定一個n*m的二維整數數組,用來表示一個迷宮,數組中只包含0或1,其中0表示可以走的路,1表示不可通過的牆壁。
最初,有一個人位於左上角(1, 1)處,已知該人每次可以向上、下、左、右任意一個方向移動一個位置。
請問,該人從左上角移動至右下角(n, m)處,至少需要移動多少次。
數據保證(1, 1)處和(n, m)處的數字爲0,且一定至少存在一條通路。
輸入格式
第一行包含兩個整數n和m。
接下來n行,每行包含m個整數(0或1),表示完整的二維數組迷宮。
輸出格式
輸出一個整數,表示從左上角移動至右下角的最少移動次數。
數據範圍
1≤n,m≤100
樣例
輸入樣例:
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
輸出樣例:
8
此題是對BFS的簡單應用,下面是3種解法和一個拓展
數組模擬
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
int n, m;
int g[N][N];//存放地圖
int d[N][N];//存 每一個點到起點的距離
PII q[N * N];//手寫隊列
int bfs()
{
int hh = 0, tt = 0;
q[0] = {0, 0};
memset(d, - 1, sizeof d);//距離初始化爲- 1表示沒有走過
d[0][0] = 0;//表示起點走過了
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};//x 方向的向量和 y 方向的向量組成的上、右、下、左
while(hh <= tt)//隊列不空
{
PII t = q[hh ++ ];//取隊頭元素
for(int i = 0; i < 4; i ++ )//枚舉4個方向
{
int x = t.first + dx[i], y = t.second + dy[i];//x表示沿着此方向走會走到哪個點
if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)//在邊界內 並且是空地可以走 且之前沒有走過
{
d[x][y] = d[t.first][t.second] + 1;//到起點的距離
q[ ++ tt ] = {x, y};//新座標入隊
}
}
}
return d[n - 1][m - 1]; //輸出右下角點距起點的距離即可
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i ++ )
for(int j = 0; j < m; j ++ )
cin >> g[i][j];
cout << bfs() << endl;
return 0;
}
queue
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
int n, m;
int g[N][N], d[N][N];
int bfs()
{
queue< pair<int, int> > q;
q.push({0, 0});
memset(d, -1, sizeof(d));
d[0][0] = 0;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while (q.size())//隊列不爲空
{
PII t = q.front();//取隊頭元素
q.pop();//出隊
for (int i = 0; i < 4; i++)
{
int x = t.first + dx[i], y = t.second + dy[i];
if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
{
d[x][y] = d[t.first][t.second] + 1;//當前點到起點的距離
q.push({x, y});//將新座標入隊
}
}
}
return d[n - 1][m -1];
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> g[i][j];
cout << bfs() << endl;
return 0;
}
python不太好寫哇,還是c++容易理解
python
def bfs():
d[0][0] = 0
queue = [(0, 0)]
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
while queue : #隊列不爲空
x, y = queue.pop(0)
for i in range(4):
a = x + dx[i];
b = y + dy[i];
if a >= 0 and a < n and b >= 0 and b < m and g[a][b] == 0 and d[a][b] == -1:
queue.append((a,b))#入隊
d[a][b] = d[x][y] + 1
print(d[n - 1][m - 1])
#main
n, m = map(int, input().split())#map函數對分割輸入後的字符列表轉換成整型
g = [[-1 for j in range(m)] for i in range(n)] # 存儲地圖
for i in range(n):
in_li = list(map(int, input().split()))
for j in range(m):
g[i][j] = in_li[j];
d = [[-1 for i in range(m)] for j in range(n)]#初始化爲 - 1
bfs()
打印路徑
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
PII q[N*N],Prev[N][N];
int g[N][N], d[N][N];
int n, m;
int bfs()
{
int hh = 0, tt = 0;
q[0] = {0, 0};
memset(d, -1, sizeof d);
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
d[0][0] = 0;
while(hh <= tt)
{
PII t = q[hh ++ ];
for(int i = 0; i < 4; i ++ )
{
int x = dx[i] + t.first, y = t.second + dy[i];
if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
{
d[x][y] = d[t.first][t.second] + 1;
Prev[x][y] = t;
q[++ tt] = {x, y};
}
}
}
int x = n - 1, y = m - 1;
while(x || y)//有一個不d等於0
{
cout << x << ' ' << y << endl;
PII t = Prev[x][y];
x = t.first, y = t.second;
}
return d[n - 1][m - 1];
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i ++ )
for(int j = 0; j < m;j ++)
cin >> g[i][j];
cout << bfs() << endl;
return 0;
}