您的位置:首页 > 其它

poj 3669 Meteor Shower

2013-12-24 14:46 309 查看
Meteor Shower
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 6806Accepted: 1985
DescriptionBessie 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 TiOutput* 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
Source CodeProblem: 3669		Memory: 1056K		Time: 79MSLanguage: C++		Result: AcceptedSource Code#include<iostream>using namespace std;const int base=1000000;int n,k=0; bool saft[400][400]={0};bool csaft[400][400]={0};int sign[4][2]={{1,0},{0,1},{-1,0},{0,-1}};typedef struct{    int x;    int y;    int t;    }node;node rect[50010];node mg[1000000];int front=-1,rear=0;bool ok=false;int cmp(const void *a,const void *b){     return (*(node *)a).t-(*(node *)b).t;   }int main(){     int x,y;     scanf("%d",&n);     for(int i=0;i<n;i++){          scanf("%d%d%d",&rect[i].x,&rect[i].y,&rect[i].t);          saft[rect[i].x][rect[i].y]=true;          if(rect[i].x>0) saft[rect[i].x-1][rect[i].y]=true;          if(rect[i].y>0) saft[rect[i].x][rect[i].y-1]=true;          saft[rect[i].x+1][rect[i].y]=true;          saft[rect[i].x][rect[i].y+1]=true;             }      qsort(rect,n,sizeof(rect[0]),cmp);     mg[0].x=0; mg[0].y=0; mg[0].t=0;     while(front<rear){           front=(front+1)%base;           while(rect[k].t<=mg[front].t){                   for(int i=0;i<4;i++){                        x=rect[k].x+sign[i][0]; y=rect[k].y+sign[i][1];                        if(x>=0 && y>=0) csaft[x][y]=true;                           }                   k++;                                          }            if(csaft[mg[front].x][mg[front].y]) continue;            if(!saft[mg[front].x][mg[front].y]){               ok=true;               break;                                               }                csaft[mg[front].x][mg[front].y]=true;           for(int i=0;i<4;i++){               x=mg[front].x+sign[i][0]; y=mg[front].y+sign[i][1];               if(x>=0 && y>=0 && !csaft[x][y]){                   rear=(rear+1)%base;                   mg[rear].x=x; mg[rear].y=y; mg[rear].t=mg[front].t+1;                       }                   }     }     if(ok==true) printf("%d\n",mg[front].t);     else printf("-1\n");     system("pause");     return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: