AcWing 844. 走迷宫
原题链接
简单
作者:
hai阿卢
,
2021-02-21 21:16:05
,
所有人可见
,
阅读 247
#include<iostream>
#include<queue>
using namespace std;
typedef pair< int, int> PII;
const int N = 110;
queue< PII> q;
int a[N][N], d[N][N], n, m;
bool s[N][N];
int BFS()
{
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
q.push({1, 1}); s[1][1] = true;
while(!q.empty())
{
PII it=q.front(); q.pop();
int x = it.first, y = it.second;
for(int i = 0; i < 4; i ++)
{
int xi = x + dx[i], yi = y + dy[i];
if(xi > 0 && xi <= n && yi > 0 && yi <= m && !a[xi][yi] && !s[xi][yi])
{
q.push({xi, yi});
d[xi][yi] = d[x][y] + 1;
s[xi][yi] = true;
if(xi == n && yi == m) return d[xi][yi];
}
}
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
cin >> a[i][j];
cout<<BFS();
return 0;
}