思路简单 但是坑点较多
思路:bfs最短路 同时处理同时砸下的陨石
坑点:
1.dist要赋INF,且Bessie的坐标是可以超过(300,300)的第一象限内任何点,题目只给了
陨石落下的坐标.
数组空间开大一点 开到陨石肯定砸不到的地方就行(301以上)
2.优先队列取时间同时 有可能在所有陨石还未处理完 bessie已经无路可走
此时bfs到的最短路 不包括未落下的陨石,会wa
因此bfs结束后还要处理剩下未落下的陨石。
3.还要注意陨石在t秒时是先于bessie落下的,因此要先处理时间再处理dist
4.输入顺序是x, y, t 看成了t x y = =
code:
#include <bits/stdc++.h>
using namespace std;
const int N = 1100, M = 5e4 + 10;
typedef pair<int, int>pii;
bool st[N][N], step[N][N];
int dist[N][N];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int n;
priority_queue<pii, vector<pii>, greater<pii>>heap;
struct node{
int x, y, t;
int id;
}star[M];
void bfs(){
queue<pii>q;
q.push({0, 0});
step[0][0] = 1;
memset(dist, 0x3f, sizeof dist);
dist[0][0] = 0;
while(q.size()){
int xx = q.front().first, yy = q.front().second;
q.pop();
auto t = heap.top();
while(t.first == 0 && heap.size()){
st[star[t.second].x][star[t.second].y] = 1;
st[star[t.second].x - 1][star[t.second].y] = 1;
st[star[t.second].x + 1][star[t.second].y] = 1;
st[star[t.second].x][star[t.second].y - 1] = 1;
st[star[t.second].x][star[t.second].y + 1] = 1;
heap.pop();
t = heap.top();
}
while(dist[xx][yy] == t.first - 1 && heap.size()){
st[star[t.second].x][star[t.second].y] = 1;
for(int i = 0;i < 4;i++){
int nx = dx[i] + star[t.second].x, ny = dy[i] + star[t.second].y;
if(nx < 0 || nx > 301 || ny < 0 || ny > 301)continue;
st[nx][ny] = 1;
}
heap.pop();
t = heap.top();
}
for(int i = 0;i < 4;i++){
int sx = dx[i] + xx, sy = dy[i] + yy;
if(st[sx][sy] ||step[sx][sy] || sx < 0 || sy < 0 || sx > 1000 || sy > 1000 )continue;
step[sx][sy] = 1;
dist[sx][sy] = dist[xx][yy] + 1;
q.push({sx, sy});
}
}
auto t = heap.top();
while(heap.size()){
st[star[t.second].x][star[t.second].y] = 1;
st[star[t.second].x - 1][star[t.second].y] = 1;
st[star[t.second].x + 1][star[t.second].y] = 1;
st[star[t.second].x][star[t.second].y - 1] = 1;
st[star[t.second].x][star[t.second].y + 1] = 1;
heap.pop();
t = heap.top();
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++){
int t, x, y;
cin >> x >> y >> t;
star[i] = {x, y, t};
heap.push({t, i});
}
bfs();
int ans = 0x3f3f3f3f;
for(int i = 0;i <= 1000;i++)
for(int j = 0;j <= 1000;j++){
if(step[i][j] && !st[i][j] && i + j >= 1){
if(dist[i][j] < ans){
ans = dist[i][j];
}
}
}
if(ans == 0x3f3f3f3f)cout << -1;
else cout << ans;
return 0;
}