记录
21:37 2023-2-9
reference:《挑战程序设计竞赛(第2版)》第二章练习题索引 p135
Description
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.
Input
- Line 1: A single integer: M
- Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti
Output
- Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
Sample Input
4
0 0 2
2 1 2
1 1 2
0 3 5
Sample Output
5
bfs。注意:
- M是50000,要使用uint32_t,否则MLE。
- BFS注意不要走回头路,否则TLE。(哈哈,我是笨蛋。
- 题目问题,看discuss板块说数据会超过300。
是一个变通一下的bfs,经典的bfs都是面对墙壁等不通的地方,这里也是,只不过多了时间。
先将被破坏的地点及破坏时间都记录下来。在dfs的时候,需要判断在当前时间路径是否无法通行,无法通行的就不进行bfs。(注意别走回头路
#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX_N 303
const int INF = 0x3f3f3f3f;
unsigned int M;
int result = -1;
typedef struct foo {
int x, y, t;
foo() {
foo(0, 0, 0);
}
foo(int x, int y, int t):x(x), y(y), t(t) {
}
} C;
C c;
int destory[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N] = {false};
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1}; //右 上 左 下
void bfs() {
std::queue<C> q;
C c(0, 0, 0);
visited[0][0] = true;
q.push(c);
while (!q.empty()) {
C c = q.front(); q.pop();
if(destory[c.x][c.y] == INF) {
result = c.t;
break;
}
for(int i = 0; i < 4; i++) {
int new_x = c.x + dx[i];
int new_y = c.y + dy[i];
if (0 <= new_x && 0 <= new_y) {
if(c.t + 1 < destory[new_x][new_y] && visited[new_x][new_y] == false)
{
q.push(C(new_x, new_y, c.t + 1));
visited[new_x][new_y] = true;
}
}
}
}
}
void init() {
if(c.t < destory[c.x][c.y]) destory[c.x][c.y] = c.t;
for(int k = 0; k < 4; k++) {
int new_x = c.x + dx[k];
int new_y = c.y + dy[k];
if (0 <= new_x && 0 <= new_y) {
if(c.t < destory[new_x][new_y])
destory[new_x][new_y] = c.t;
}
}
}
void solve() {
bfs();
printf("%d\n", result);
}
int main() {
std::fill(destory[0], destory[0] + MAX_N * MAX_N, INF);
scanf("%d", &M);
for(int i = 0; i < M; i++) {
scanf("%d %d %d", &c.x, &c.y, &c.t);
init();
}
solve();
}
标签:--,MAX,3669,bfs,int,meteor,time,include
From: https://www.cnblogs.com/57one/p/17107210.html