广度优先搜索
洛谷P2895
[USACO08FEB] Meteor Shower S
题面翻译
题目描述
贝茜听说一场特别的流星雨即将到来:这些流星会撞向地球,并摧毁它们所撞击的任何东西。她为自己的安全感到焦虑,发誓要找到一个安全的地方(一个永远不会被流星摧毁的地方)。
如果将牧场放入一个直角坐标系中,贝茜现在的位置是原点,并且,贝茜不能踏上一块被流星砸过的土地。
根据预报,一共有 $M$ 颗流星 $(1\leq M\leq 50,000)$ 会坠落在农场上,其中第 $i$ 颗流星会在时刻 $T_i$($0 \leq T _ i \leq 1000$)砸在坐标为 $(X_i,Y_i)(0\leq X_i\leq 300$,$0\leq Y_i\leq 300)$ 的格子里。流星的力量会将它所在的格子,以及周围 $4$ 个相邻的格子都化为焦土,当然贝茜也无法再在这些格子上行走。
贝茜在时刻 $0$ 开始行动,她只能在会在横纵坐标 $X,Y\ge 0$ 的区域中,平行于坐标轴行动,每 $1$ 个时刻中,她能移动到相邻的(一般是 $4$ 个)格子中的任意一个,当然目标格子要没有被烧焦才行。如果一个格子在时刻 $t$ 被流星撞击或烧焦,那么贝茜只能在 $t$ 之前的时刻在这个格子里出现。 贝茜一开始在 $(0,0)$。
请你计算一下,贝茜最少需要多少时间才能到达一个安全的格子。如果不可能到达输出 $−1$。
输入格式
共 $M+1$ 行,第 $1$ 行输入一个整数 $M$,接下来的 $M$ 行每行输入三个整数分别为 $X_i, Y_i, T_i$。
输出格式
贝茜到达安全地点所需的最短时间,如果不可能,则为 $-1$。
题目描述
Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.
The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.
Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).
Determine the minimum time it takes Bessie to get to a safe place.
输入格式
* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti
输出格式
* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
样例 #1
样例输入 #1
4
0 0 2
2 1 2
1 1 2
0 3 5
样例输出 #1
5
可先看我的另一个博客
我们不需要根据时间来模拟,而是可以记录每个点的最早被影响时间,随后再进行搜索
这里写几个坑点:
- 初始化time数组为-1,不能为0,因为可能陨石在0时刻就降落
- 陨石先出现,影响了五个点,这五个点可能会被后面的陨石影响
比如,先输入1,1,5 ,(1,1),(0,1),(1,0),(1,2),(2,1)这5个点的time就会被覆盖为5,如果后输入2,1,3,那么(1,1)和(2,1)的time就要改为3。 - 还是像我的这个博客一样,超时问题
下面是ac代码
#include<bits/stdc++.h>
using namespace std;
struct pos {
int x, y;
int time;
};
int main() {
int m;
int time[310][310], flag[310][310] = {0};
memset(time, -1, sizeof(time));
int di[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
cin >> m;
for(int i = 0; i < m; i++) {
int x, y, t;
cin >> x >> y >> t;
if(time[x][y] == -1){
time[x][y] = t;
}
for(int j = 0; j < 4; j++) {
if(x+di[j][0] >= 0 && y+di[j][1] >= 0) {
if(time[x+di[j][0]][y+di[j][1]] == -1 || t < time[x+di[j][0]][y+di[j][1]]){
time[x+di[j][0]][y+di[j][1]] = t;
}
}
}
}
pos p; p.x = 0; p.y = 0; p.time = 0;
queue <pos> q;
q.push(p);
while(!q.empty() && time[q.front().x][q.front().y] != -1) {
int nx = q.front().x, ny = q.front().y, ntime = q.front().time;
for(int i = 0; i < 4; i++) {
if(nx+di[i][0] < 0 || ny+di[i][1] < 0) continue;
if(!flag[nx+di[i][0]][ny+di[i][1]]) {
if(time[nx+di[i][0]][ny+di[i][1]] == -1 || time[nx+di[i][0]][ny+di[i][1]] > ntime+1) {
pos temp; temp.x = nx+di[i][0]; temp.y = ny+di[i][1]; temp.time = ntime + 1;
flag[temp.x][temp.y] = 1;
q.push(temp);
}
}
}
q.pop();
}
if(q.empty()) cout << -1;
else cout << q.front().time << endl;
system("pause");
}
标签:洛谷,temp,di,int,贝茜,P2895Meteor,Shower,leq,time
From: https://www.cnblogs.com/rjxq/p/18206882