您的位置:首页 > 其它

POJ 3669 Meteor Shower 《挑战程序设计竞赛(第2版)》练习题

2014-11-20 14:32 447 查看
Meteor Shower

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 9193Accepted: 2601
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 (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
4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output
5

虽然不是啥难题,但是毕竟第一次写BFS,毕竟新手,写了好久,语法错误都改了半天、、、、、、、、、、总之还好,比较麻烦的是要模拟流星雨不同时间落下,容易错。。

代码如下

#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<set>
#include<ctime>
#include<cctype>
#include<memory>
#include<cstdlib>
#include<map>
#include<queue>
#include<stack>
#include<climits>
#include<list>
using namespace std;
struct point
{
    int x;
    int y;
    int t;
};
int mp[400][400];
bool mp1[400][400];
point pt[50020];
int mv[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
void mark1(int x,int y)///标记危险点
{
    int dx,dy;
    mp1[x][y]=1;
    for(int i=0; i<4; i++)
    {
        dx=x+mv[i][0];
        dy=y+mv[i][1];
        if(0<=dx&&0<=dy)
            mp1[dx][dy]=1;
    }
    return;
}
void mark2(int x,int y)//标记不可走点
{
    int dx,dy;
    mp[x][y]=-2;
    for(int i=0; i<4; i++)
    {
        dx=x+mv[i][0];
        dy=y+mv[i][1];
        if(0<=dx&&0<=dy)
            mp[dx][dy]=-2;
    }
    return;
}
bool cmp(point a,point b)
{
    return a.t<=b.t;
}
int main()
{
    int M;
    while(cin>>M)
    {
        bool fund=0;
        queue<point> loc;
        memset(mp,-1,sizeof(mp));
        memset(mp1,0,sizeof(mp1));
        for(int i=0; i<M; i++)
            scanf("%d%d%d",&pt[i].x,&pt[i].y,&pt[i].t);
        sort(pt,pt+M,cmp);////把流星雨按下落时间排序
        for(int i=0; i<M; i++)
            mark1(pt[i].x,pt[i].y);
        int time=0;
        point now,temp;
        now.x=0;
        now.y=0;
        now.t=0;
        mp[0][0]=0;
        loc.push(now);
        int fr=0,ed=-1;
        while(true)
        {
            if(loc.empty())///队列为空无路可走,退出
                break;
            now=loc.front();
            loc.pop();
            if(now.t==time)
            {
                time++;////流星雨落下一波
                while(pt[ed].t<=time&&ed+1<=M)
                    ed++;
                for(int i=fr; i<ed; i++)
                    mark2(pt[i].x,pt[i].y);
                fr=ed;
            }
            if(!mp1[now.x][now.y])///当前点安全,退出
            {
                fund=1;
                break;
            }
            int dx,dy;
            for(int i=0; i<4; i++)
            {
                dx=now.x+mv[i][0];///探路,入队列
                dy=now.y+mv[i][1];
                if(dx>=0&&dy>=0&&mp[dx][dy]!=-2)
                {
                    if((time<mp[dx][dy])||(mp[dx][dy]==-1))
                    {
                        mp[dx][dy]=time;
                        temp.x=dx;
                        temp.y=dy;
                        temp.t=time;
                        loc.push(temp);
                    }
                }
            }
        }
        if(fund)
            cout<<now.t<<endl;
        else
            cout<<-1<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: