#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 310;
typedef pair<int, int> PII;
PII q[N * N];
int fire[N][N];
int dist[N][N];
int dx[] = { -1,0,1,0 };
int dy[] = { 0,-1,0,1 };
int hh = 0, tt = 0;
int bfs() {
q[0] = { 0,0 };
dist[0][0] = 0;
while (hh <= tt) {
auto w = q[hh++];
for (int i = 0; i < 4; i++) {
int a = w.first + dx[i], b = w.second + dy[i];
if (a < 0 || b < 0) continue;
if (dist[a][b]) continue;
if (dist[w.first][w.second] + 1 >= fire[a][b]) continue;
dist[a][b] = dist[w.first][w.second] + 1;
q[++tt] = { a,b };
if (fire[a][b] > 1e9) return dist[a][b];
}
}
return -1;
}
int main() {
int n, x, y, t;
memset(fire, 0x3f, sizeof fire);
cin >> n;
while (n--) {
cin >> x >> y >> t;
fire[x][y] = min(t, fire[x][y]);
for (int i = 0; i < 4; i++) {
int c = x + dx[i], d = y + dy[i];
if (c < 0 || d < 0 || c>301 || d>301) continue;
fire[c][d] = min(t, fire[c][d]);
}
}
int res = bfs();
cout << res;
return 0;
}
P2895 Meteor Shower S
最新推荐文章于 2024-08-31 15:34:19 发布