AcWing 1374. 穿越栅栏
原题链接
简单
作者:
ywt51
,
2024-11-20 14:35:08
,
所有人可见
,
阅读 1
//多源最短路 两个源点的最短路
//存图和判断
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 210;
int n, m, res, d[N][N];
string g[N];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
queue<PII> q;
int main()
{
cin >> m >> n;
n = n * 2 + 1;
m = m * 2 + 1;
string t;
getline(cin, t);
for (int i = 0; i < n; ++ i)
{
getline(cin, t);
g[i] = t;
}
memset(d, -1, sizeof d);
//找到两个出口
for (int i = 0; i < n; ++ i)
for (int j = 0; j < m; ++ j)
{
if (i != 0 && i != n-1 && j != 0 && j != m-1) continue;
if (g[i][j] != '+' && g[i][j] != '-' && g[i][j] != '|')
{
q.push({i, j});
d[i][j] = 0;
}
}
while (q.size())
{
auto 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) continue;
if (g[x][y] != ' ' || d[x][y] != -1) continue;
d[x][y] = d[t.first][t.second] + 1;
q.push({x, y});
res = max(res, d[x][y]);
}
}
cout << (res + 1) / 2; //迷宫内的移动,两步算真实的一步,最后出去的1步,算1步, 总共加起来除以2
return 0;
}