POJ3669 Meteor Shower (BFS)

来源:互联网 发布:在淘宝当主播怎么赚钱 编辑:程序博客网 时间:2024/05/22 17:34
Meteor Shower
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20419 Accepted: 5294

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

Source

USACO 2008 February Silver

题目大意如下:天空中即将出现流星雨,第i个流星会落在坐标(xi,yi)处在时间ti,并且流星不仅会破坏(xi,yi)处,与其相连的4个点也会遭到破坏。刚开始Bessie在(0,0)坐标处,每秒只能够走一个点,并且被破坏的点不能经过,求Bessie到达一个安全地方的最少时间。
思路:首先应该标记所有会被流星击中的点,其次标记被流星波及到的点的坐标,然后进行BFS搜索,直到有点的坐标满足初始条件即可说明该点是安全的。


AC代码如下:
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;const int MAX_N = 305;const int MAX_M = 50005;int X[MAX_M],Y[MAX_M],T[MAX_M];int maze[MAX_N][MAX_N],dis[MAX_N][MAX_N];int m,dir[4][2] = {-1,0,0,-1,1,0,0,1};typedef pair<int, int> P;int bfs(){queue<P> q;dis[0][0] = 0;q.push(P(0,0));if(maze[0][0] == 0)//在起点直接就被砸中,则直接over return -1;while(!q.empty()){P p = q.front();q.pop();int x = p.first, y = p.second;if(maze[x][y] == INF)//当前坐标处于安全区域 return dis[x][y];for(int i = 0; i < 4; i ++){//判断相邻的四个方向 int sx = x + dir[i][0], sy = y + dir[i][1];//没越界并且该点没有被访问过并且该点在下一秒或者以前都没有被流星击中过 if(sx >= 0 && sy >= 0 && dis[sx][sy] == INF && dis[x][y] + 1 < maze[sx][sy]) q.push(P(sx,sy)), dis[sx][sy] = dis[x][y] + 1;}}return -1;}void solve(){for(int i = 0; i < MAX_N; i ++)//初始化maze[][]以及dis[][] fill(maze[i],maze[i]+MAX_N,INF),fill(dis[i],dis[i]+MAX_N,INF); for(int i = 0; i < m; i ++){maze[X[i]][Y[i]] = min(maze[X[i]][Y[i]],T[i]);//选择较早的击中时间 for(int j = 0; j < 4; j ++){int sx = X[i] + dir[j][0],sy = Y[i] + dir[j][1];if(sx >= 0 && sy >= 0)//被流星集中点的相邻四点,仍然选择最早的爆炸时间 maze[sx][sy] = min(maze[sx][sy],T[i]); }}int ans = bfs();printf("%d\n",ans);}int main(){while(~scanf("%d",&m)){for(int i = 0; i < m; i ++){scanf("%d%d%d",&X[i],&Y[i],&T[i]);}solve();}return 0;}


原创粉丝点击