ACM Meteor Shower(挑战程序设计竞赛)
来源:互联网 发布:口算生成器 软件下载 编辑:程序博客网 时间:2024/05/29 13:10
Meteor Shower
64-bit integer IO format: %lld Java class name: Main
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
40 0 22 1 21 1 20 3 5
Sample Output
5
Source
#include <iostream>#include <cstdio>#include <cstdlib>using namespace std;#define MAX_X 310#define MAX_T 1001#define MAX_M 50010#define MAX_Q 100000#define ADD(x) x=(x+1)%MAX_Qstruct Node{int x,y;int time;} queue[MAX_Q];struct Dat{int x,y,t;} Data[MAX_M];int front,rear;int M;int dx[]={1,-1,0,0};int dy[]={0,0,1,-1};bool map[MAX_X][MAX_X];bool nosafe[MAX_X][MAX_X];bool visited[MAX_X][MAX_X];bool dT[MAX_T];int cmp(const void *a,const void *b){Dat *pa,*pb;pa=(Dat *)a;pb=(Dat *)b;return pa->t-pb->t;}int main(){int now=0;scanf("%d",&M);for(int i=0;i<M;i++){scanf("%d%d%d",&Data[i].x,&Data[i].y,&Data[i].t);nosafe[Data[i].x][Data[i].y]=true;for(int j=0;j<4;j++){int nx=Data[i].x+dx[j],ny=Data[i].y+dy[j];if(nx>=0 && nx<=301 && ny>=0 && ny<=301)nosafe[nx][ny]=true;}}qsort(Data,M,sizeof(Dat),cmp);front=rear=0;queue[rear]=Node{0,0,0};visited[0][0]=true;ADD(rear);int ans=-1;Node q;while(front!=rear){q=queue[front];ADD(front);if(!dT[q.time]){dT[q.time]=true;for(;Data[now].t==q.time;now++){map[Data[now].x][Data[now].y]=true;for(int i=0;i<4;i++){int nx=Data[now].x+dx[i],ny=Data[now].y+dy[i];if(nx>=0 && nx<=301 && ny>=0 && ny<=301){map[nx][ny]=true;}}}}if(map[q.x][q.y]){continue;}if(!nosafe[q.x][q.y]){ans=q.time;break;}for(int i=0;i<4;i++){int nx=q.x+dx[i],ny=q.y+dy[i];if(nx>=0 && nx<=301 && ny>=0 && ny<=301 && !visited[nx][ny] && !map[nx][ny]){queue[rear]=Node{nx,ny,q.time+1};ADD(rear);visited[nx][ny]=true;}}}cout<<ans<<endl;return 0;}
- ACM Meteor Shower(挑战程序设计竞赛)
- [挑战程序设计竞赛] POJ 3699 - Meteor Shower
- POJ 3669-Meteor Shower [bfs] 《挑战程序设计竞赛》2.1
- POJ 3669 Meteor Shower 《挑战程序设计竞赛(第2版)》练习题
- ACM Ball(挑战程序设计竞赛)
- ACM Hopscotch(挑战程序设计竞赛)
- ACM Osenbei(挑战程序设计竞赛)
- ACM Packets(挑战程序设计竞赛)
- ACM Red and Black(挑战程序设计竞赛)
- ACM Property Distribution(挑战程序设计竞赛)
- ACM Curling 2.0(挑战程序设计竞赛)
- ACM Seven Puzzle(挑战程序设计竞赛)
- ACM Smallest Difference(挑战程序设计竞赛)
- ACM Backward Digit Sums(挑战程序设计竞赛)
- ACM Cleaning Shifts(挑战程序设计竞赛)
- ACM Radar Installation(挑战程序设计竞赛)
- ACM Stall Reservations(挑战程序设计竞赛)
- ACM Yogurt factory(挑战程序设计竞赛)
- 深入浅出UML类图
- dojo chart生成函数
- 第七周项目一串联电阻
- 理解九种图
- UVA111 (DP)
- ACM Meteor Shower(挑战程序设计竞赛)
- 聚内核与微内核
- 第七周项目3(1)-求两数正差值
- uva-301 - Transportation
- 怎么使用jquery阻止页面的离开或卸载
- Handler详解系列(六)——View的post()方法详解
- 常见开源加密算法库
- 【Linux】调整ubuntu10.04 窗口关闭按钮的位置
- 2014-10-12 学习日志