AcWing 844. 走迷宫
原题链接
简单
作者:
dsyami
,
2021-06-04 13:18:33
,
所有人可见
,
阅读 365
#include <iostream>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int n, m, st[N][N];
int g[N][N];
PII queue[N * N];
void bfs()
{
int hh = 0, tt = -1;
queue[++ tt] = {1, 1};
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while (hh <= tt)
{
int x = queue[hh].first, y = queue[hh].second;
hh ++;
for (int i = 0; i < 4; i ++ )
{
if (!g[x + dx[i]][y + dy[i]] && !st[x + dx[i]][y + dy[i]] && x + dx[i] > 0 && x + dx[i] <= n && y + dy[i] > 0 && y + dy[i] <= m)
{
queue[++ tt] = {x + dx[i], y + dy[i]};
st[x + dx[i]][y + dy[i]] = st[x][y] + 1;
}
}
}
cout << st[n][m];
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
scanf("%d", &g[i][j]);
bfs();
return 0;
}