POJ 3669 Meteor Shower(bfs)

来源:互联网 发布:上古卷轴5ece捏脸数据 编辑:程序博客网 时间:2024/05/17 03:37
Meteor Shower
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 19166 Accepted: 4979

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

tips:
1.图中存取的每个点的值是流星撞击到此点的最短时间。
2.主人公只能在此点为被撞击前经过此点
3.题目并没有给出上限,但是我们可以判断出在流星最多撞击到点301,301处
4.因为题目中给定的时间可以为0,所以一开始初始化为-1,表示不会被撞击到
#include<iostream>#include<queue>#include<vector> #include<cstring>#include<cstdio>using namespace std;int t[311][311];int m;struct node{int x,y,step;};int book[311][311];int dire[4][2]={-1,0,1,0,0,-1,0,1};int bfs(){int ans=-1;queue<node>q;q.push(node{0,0,0});book[0][0]=1;while(!q.empty()){node tt=q.front();q.pop();int x=tt.x;int y=tt.y;int step=tt.step;if(t[x][y]==-1){ans=step;break;}for(int i=0;i<4;i++){int nx=x+dire[i][0];int ny=y+dire[i][1];if(nx<0||ny<0)continue;if(!book[nx][ny]&&(step+1<t[nx][ny])||t[nx][ny]==-1){book[nx][ny]=1;q.push(node{nx,ny,step+1});}}}return ans; }int main(){cin>>m;memset(t,-1,sizeof(t));for(int i=1;i<=m;i++){int x,y,tt;scanf("%d%d%d",&x,&y,&tt);if(t[x][y]==-1)t[x][y]=tt;else if(t[x][y]>=0)t[x][y]=min(t[x][y],tt);for(int j=0;j<4;j++){int nx=x+dire[j][0];int ny=y+dire[j][1];if(nx<0||ny<0)continue;if(t[nx][ny]==-1)t[nx][ny]=tt;else if(t[nx][ny]>=0)t[nx][ny]=min(t[nx][ny],tt);}}printf("%d\n",bfs());return 0;}



0 0
原创粉丝点击