POJ 3669 Meteor Shower (bfs)

来源:互联网 发布:vscode nodejs 调试 编辑:程序博客网 时间:2024/05/17 07:31

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536KTotal 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

40 0 22 1 21 1 20 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 LOCALfreopen("in.txt", "r", stdin);//freopen("out.txt","w",stdout);#endifios_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;}


0 0
原创粉丝点击