POJ3669 解题报告

来源:互联网 发布:巨邦网络 编辑:程序博客网 时间:2024/06/03 21:18
Meteor Shower
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 20461Accepted: 5312

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: Linei+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.

Sample Input

40 0 22 1 21 1 20 3 5

Sample Output

5

Source

USACO 2008 February Silver

这题较为简单,一遍过

我的做法是BFS

下面贴代码,注释较全

#include<iostream>#include<algorithm>#include<queue>using namespace std;struct block{    //使用结构体block来记录到达点(x,y)时走的步数    int x;    int y;    int step;};typedef block B;const int INF=2e9;const int maxn=330;int dx[4]= {-1,0,1,0};int dy[4]= {0,1,0,-1};  //方向int status[maxn][maxn]; //记录每个点被摧毁的最小时间,永远不会被摧毁则为INFint bfs();    //核心函数void show();  //show()函数是为了方便调试的,可以帮助看到数组status里面元素的值int main(){    int m;    int x,y,t;    while(cin>>m)    {        fill(status[0],status[0]+maxn*maxn,INF);        while(m--)  //处理接下来的m颗流星        {            cin>>x>>y>>t;            if(status[y][x]>t) status[y][x]=t;   //设置输入位置被摧毁的时间            for(int i=0; i<4; i++)               //设置输入位置相邻的四个点的摧毁时间            {                int nx=x+dx[i],ny=y+dy[i];                if(0<=nx&&0<=ny&&status[ny][nx]>t)                    status[ny][nx]=t;            }        }        //show();        cout<<bfs()<<endl;        //show();    }    return 0;}int bfs(){    queue<B> que;      //使用队列来实现bfs    B s;               //结构体变量s临时存储一个结构体,方便加入队列中,我本来是使用que.push(B(0,0,0))这样的形式的,但是行不通,郁闷    s.x=s.y=s.step=0;    que.push(s);    while(!que.empty())  //当队列为空时说明没有可以走的点了,没有安全的地方了,也就停止循环了,直接跳到最后返回-1    {        B b=que.front(); //从队列中取出一个点        que.pop();       //将取出的点从队列中清除        if(status[b.y][b.x]==INF) return b.step; //如果取出的点的status值为INF说明这个点是安全的,那我们就得到答案了        if(status[b.y][b.x]<=b.step) continue;   //如果status值为-1,说明已经访问过了,可以跳过这个点了        status[b.y][b.x]=-1;  //将现在访问的点标记为访问过的状态        for(int i=0;i<4;i++)  //将周围四个点中符合条件的加入队列        {            int nx=b.x+dx[i],ny=b.y+dy[i];            if(nx>=0&&ny>=0&&status[ny][nx]!=-1)            {                s.x=nx; s.y=ny; s.step=b.step+1;                que.push(s);            }        }    }    return -1;   //无法到达安全的点,主人公死掉了。。。。。。。。。。。返回-1}void show(){    for(int i=0;i<6;i++)    {        for(int j=0;j<6;j++)            if(status[i][j]<INF) cout<<status[i][j]<<"   ";            else cout<<"inf ";        cout<<endl;    }}

原创粉丝点击