POJ 3669 Meteor Shower

来源:互联网 发布:照片修改软件在线 编辑:程序博客网 时间:2024/06/04 17:34
Meteor Shower
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15092 Accepted: 3971

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 meteori will striking point (Xi,Yi) (0 ≤ Xi≤ 300; 0 ≤ Yi ≤ 300) at timeTi (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, andTi

Output

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.



简单的宽搜题,真的很简单,简单到自己WA13次。。。

题目大意就是世界末日到了,有m个陨石块掉下来要毁灭世界,每个陨石块会毁灭自己的坐标和上下左右的坐标。给出每个陨石降落的坐标和时间,要求走到安全地点的最短路径。(安全地点指永远都不会被陨石袭击)

做法就是宽搜,预处理。注意这道题因为没必要走回头路,所以visit开两维就好了。然后预处理每个坐标受到袭击的最早时间(初始化为最大值),然后宽搜,搜到安全地带退出。

注意两种特殊情况,一种是在时间0陨石砸到了(0,0)或者(0,1)或者(1,0),那就直接gg了;另一种是(0,0)本身是安全的,走0步。这两种特殊情况容易出错,是因为我们初始化的时候没有想好,在bfs中的规则也要应用到初始化中。

然后我WA了13次的原因是bfs里括号范围写错了。。。。


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<queue>#include<math.h>#include<algorithm>using namespace std;struct myk{int x;int y;int time;};myk storm[50000+10];queue <myk> q;const int dx[]={-1,1,0,0};const int dy[]={0,0,-1,1};int ans=-1,max_time=-1,m,b[1010][1010],visit[1010][1010];//b[x][y][t] stands for whether coordinate (x,y) is safe at time t//max_time stands for the greatest time of storm hit(after which there will be no more storm hit)bool iseligble(int x,int y){    if((x>=0)&&(y>=0)&&(x<=1010)&&(y<=1010))        return true;    else        return false;}void push(int xx,int yy,int tt){    myk temp;    temp.x=xx;    temp.y=yy;    temp.time=tt;    q.push(temp);    visit[xx][yy]=1;}bool issafe(int x,int y){    if(b[x][y]==10000)        return true;    else        return false;}void bfs(void){    int x_now,y_now,t_now,xx,yy,tt;    myk temp;    while(!q.empty())    {        temp=q.front();        x_now=temp.x;        y_now=temp.y;        t_now=temp.time;        q.pop();        //printf("(%d,%d):%d\n",x_now,y_now,t_now);        for(int i=0;i<4;i++)        {            xx=x_now+dx[i];            yy=y_now+dy[i];            tt=t_now+1;            if((iseligble(xx,yy))&&((b[xx][yy]>tt))&&(!visit[xx][yy]))            {                //step[xx][yy]=tt;                if((issafe(xx,yy)))                {                    ans=tt;                    return ;                }                push(xx,yy,tt);            }        }    }}int main(void){    int xx,yy,tt;    cin>>m;    //memset(b,-1,sizeof(b));    memset(visit,0,sizeof(visit));    //memset(step,-1,sizeof(step));    for(int i=0;i<1010;i++)        for(int j=0;j<1010;j++)            b[i][j]=10000;    //printf("b[0][1]=%d\nb[1][0]=%d\n",b[0][1],b[1][0]);    for(int i=0;i<m;i++)    {        scanf("%d%d%d",&storm[i].x,&storm[i].y,&storm[i].time);        max_time=max(max_time,storm[i].time);        b[storm[i].x][storm[i].y]=min(storm[i].time,b[storm[i].x][storm[i].y]);        //printf("1:b[0][1]=%d\nb[1][0]=%d\n",b[0][1],b[1][0]);        for(int j=0;j<4;j++)        {            xx=storm[i].x+dx[j];            yy=storm[i].y+dy[j];            tt=storm[i].time;            if(iseligble(xx,yy))                b[xx][yy]=min(b[xx][yy],tt);            //printf("2:b[0][1]=%d\nb[1][0]=%d\n",b[0][1],b[1][0]);        }    }    //printf("b[0][1]=%d\nb[1][0]=%d\n",b[0][1],b[1][0]);    if(issafe(0,0))        printf("0\n");    else    {        if(b[0][0]==0)            printf("-1\n");        else        {            push(0,0,0);            //visit[0][0]=0;            bfs();            printf("%d\n",ans);        }    }    //step[0][0]=0;    return 0;}

0 0