POJ 3669 Meteor Shower (bfs)

在这篇博客中,我们探讨了一项独特的任务:帮助奶牛Bessie找到一个安全的地方,避免流星雨的袭击。通过预处理流星撞击时间和位置,我们应用广度优先搜索算法来计算Bessie最快到达安全地点所需的时间。了解如何在有限时间内规划路径,避免危险,为解决现实世界中的紧急情况提供灵感。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9896 Accepted: 2762

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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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: XiYi, 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

先预处理每个格子最早的毁坏时间 然后从(0,0) bfs求最短的到达安全位置的时间即可 由于没有标记是否访问 记得刷新毁坏的时间来保证每个格子只访问一次

本题有个坑点的就是 我想当然了 由于流星落在(XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 300) 的范围内 便以为安全的格子就在这个范围内 事实上安全的格子是第一象限的任意位置

所以边界条件是if(nx < 0 || ny < 0) continue;

AC代码如下:

//
//  POJ 3669 Meteor Shower
//
//  Created by TaoSama on 2015-02-20
//  Copyright (c) 2014 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

struct Point {
	int x, y, t;
	Point(int x = 0, int y = 0, int t = 0): x(x), y(y), t(t) {}
};
int n, a[305][305];
int d[5][2] = {0, -1, 0, 1, 1, 0, -1, 0, 0, 0};
int bfs() {
	queue<Point> q; q.push(Point(0, 0));
	while(!q.empty()) {
		Point cur = q.front(); q.pop();
		for(int i = 0; i < 4; ++i) {
			Point nxt(cur.x + d[i][0], cur.y + d[i][1], cur.t + 1);
			if(nxt.x < 0 || nxt.y < 0) continue;
			if(a[nxt.x][nxt.y] == INF) return nxt.t;
			if(nxt.t < a[nxt.x][nxt.y]) {
				a[nxt.x][nxt.y] = nxt.t;
//				cout<<"nxt: "<<nxt.t<<endl;
				q.push(nxt);
			}
		}
	}
	return -1;
}
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	ios_base::sync_with_stdio(0);

	while(scanf("%d", &n) == 1) {
		memset(a, 0x3f, sizeof a);
		for(int i = 1; i <= n; ++i) {
			int x, y, t; scanf("%d%d%d", &x, &y, &t);
			for(int j = 0; j < 5; ++j) {
				int nx = x + d[j][0], ny = y + d[j][1];
				if(nx < 0 || ny < 0) continue;
				a[nx][ny] = min(t, a[nx][ny]);
			}
		}
		int ans = bfs();
		printf("%d\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值