poj 3669 Meteor Shower

Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20463   Accepted: 5313

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
/*
* @Author: Lyucheng
* @Date:   2017-07-17 10:13:52
* @Last Modified by:   Lyucheng
* @Last Modified time: 2017-07-19 09:37:42
*/
/*
 题意:牛要吃草,初始位置在(0,0),有m个流星,每个流星会在Ti时间点降落在(Xi,Yi)点,周围的一个区域会被炸毁,
    问你牛想要到达一个从来没有被炸毁的地方最少需要走多久,走一步需要一个时间单位,如果没法走到一个安全的
    位置就输出-1

 思路:将流星按照时间排序,然后用一个地图标记,要从后往前标记,因为第二次炸毁的时候,没有意义
    然后就是简单的BFS
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define MAXN 50005
#define MAXM 305
#define INF 1000
using namespace std;

struct Node{
    int x,y,t;
    bool operator < (const Node & other) const{
        return t<other.t;
    }
}node[MAXN];
struct Point{
    int x,y,step;
    Point(){}
    Point(int _x,int _y,int _step):x(_x),y(_y),step(_step){}
};
int n;
bool vis[MAXM][MAXM];
int mapn[MAXM][MAXM];
int dir[5][2]={1,0,-1,0,0,1,0,-1,0,0};

bool ok(int x,int y){
    if(x<0||y<0) return false;
    return true;
}

int bfs(int x,int y,int step){
    Point start=Point(x,y,step);
    Point Next;
    queue<Point>q;
    q.push(start);
    vis[x][y]=true;
    while(!q.empty()){
        Next=q.front();q.pop();
        for(int i=0;i<4;i++){
            int fx=Next.x+dir[i][0];
            int fy=Next.y+dir[i][1];
            if (ok(fx,fy) && mapn[fx][fy]>Next.step+1 && vis[fx][fy]==false) {
                vis[fx][fy]=true;
                if (mapn[fx][fy]==INF)//走到空地了
                    return Next.step+1;
                q.push(Point(fx,fy,Next.step+1));
            }

        }
    }
    return -1;
}

void init(){
    memset(vis,false,sizeof vis);
}

int main(){ 
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    while(scanf("%d",&n)!=EOF){
        init();
        for(int i=0;i<n;i++){
            scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].t);
        }

        for(int i=0;i<MAXM;i++){
            for(int j=0;j<MAXM;j++){
                mapn[i][j]=INF;
            }
        }

        sort(node,node+n);
        //从后往前标记,因为炸第二次其实没意义
        for(int i=n-1;i>=0;i--){
            for(int j=0;j<=4;j++){
                int fx=node[i].x+dir[j][0];
                int fy=node[i].y+dir[j][1];
                if(ok(fx,fy)){
                    mapn[fx][fy]=node[i].t;
                }                
            }
        }

        if(mapn[0][0]==0){
            puts("-1");
            continue;
        }
        printf("%d\n",bfs(0,0,0));
    }
    return 0;
}

 

posted @ 2017-07-19 09:39  勿忘初心0924  阅读(142)  评论(0编辑  收藏  举报