***POJ 3669 Meteor Shower【流星雨】

原文链接
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

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

注意流星不攻击起点的情况。

AC代码1:手动队列

#include <stdio.h>
#include <iostream> 
#define INF 999999999
using namespace std;

struct xyt{
    int x,y,time;
}que[5000000],temp;
//que:每一点的位置以及到到达该点的最短时间 
bool vis[401][401];
//标记经过的位置,不走重复路线 
int des[401][401];
//该位置被炸毁的时间 
int n,top,bottom,next[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
//n:流星数 
//next:上下左右移动 

int search()
{
    int i,j;
    que[0].x=que[0].y=que[0].time=0;  
    //起始位置0 
    top=0;
    bottom=1;
    if(des[0][0]==INF)  return 0;  
    //如果原点永远不被击中,则不必离开,即最短时间为0 
    if(des[0][0]==0)  return -1;  
    //如果原点第0秒就被流星击中,则无法安全离开,直接返回-1 
    vis[0][0]=1;
    while(top<bottom){
        if(des[que[top].x][que[top].y]==INF)  return que[top].time;  
        //如果该点永远不被击中,则可作为终点,返回到达该点的最短时间 
        else if(que[top].time<des[que[top].x][que[top].y]){  
        //如果到达该点时,该点还没有被击中 
            for(i=0;i<4;i++){  //上下左右移动 
                temp.x=que[top].x+next[i][0];
                temp.y=que[top].y+next[i][1]; 
                if(temp.x<401 && temp.x>=0 && temp.y<401 && temp.y>=0 && !vis[temp.x][temp.y] && des[temp.x][temp.y]>que[top].time+1){ 
                //如果该点不超出范围,未经过,未被击中 
                    vis[temp.x][temp.y]=1;
                    //经过该点,标记1 
                    que[bottom].x=temp.x;
                    que[bottom].y=temp.y;
                    que[bottom].time=que[top].time+1;
                    bottom++;
                }
            }
        }
        top++;
    }
    return -1;
}

int main()
{
    int i,j,x,y,time;
    cin>>n;
    getchar();
    for(i=0;i<401;i++){ 
        for(j=0;j<401;j++){
            des[i][j]=INF;
            vis[i][j]=0;
        }
    }
    for(i=0;i<n;i++){
        cin>>x>>y>>time;
        getchar(); 
        if(time<des[x][y]) des[x][y]=time;
        for(j=0;j<4;j++){
            temp.x=x+next[j][0];
            temp.y=y+next[j][1]; 
            if(temp.x<401 && temp.x>=0 && temp.y<401 && temp.y>=0){
                if(time<des[temp.x][temp.y])  des[temp.x][temp.y]=time;
            }
        }
    }
    printf("%d\n",search());
    return 0;
}

AC代码2:队列

#include <cstdio>  
#include <cstring>  
#include <algorithm>  
#include <iostream>  
#include <queue>  
using namespace std;

const int maxn = 405;   
const int INF = 1<<29; 

struct Node  
{  
    int x;   
    int y;   
    int t;   
    bool operator < (const Node &a)const  
    {  
    return t < a.t;   
    }  
}a[51000];   

int n,movex[4] = {0,0,-1,1},movey[4] = {1,-1,0,0};   
queue <Node> q;   
int dist[maxn][maxn];

bool range(int x)  
{  
    if(x < 0 || x >= maxn)  return false;   
    else return true;   
}  

int main()  
{  
    while(scanf("%d", &n) != EOF){  
    for(int i = 0; i < n; i++)  scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].t);   
    for(int i = 0; i < maxn; i++){  
        for(int j = 0; j < maxn; j++){  
            dist[i][j] = INF;  
        }
    }
    sort(a, a + n);   
    Node node;   
    node.x = 0;   
    node.y = 0;   
    node.t = 0;   
    dist[0][0] = 0;   
    q.push(node);   
    int index = 0;   
    while(!q.empty()){  
        int x = q.front().x;   
        int y = q.front().y;   
        int t = q.front().t;   
        q.pop();   
        while(index < n && a[index].t <= t){  
        dist[a[index].x][a[index].y] = -1;   
        for(int i = 0; i < 4; i++)  
            if(range(a[index].x + movex[i]) && range(a[index].y + movey[i])){
                dist[a[index].x + movex[i]][a[index].y + movey[i]] = -1;
            }     
            index++;   
        }  
        if(dist[x][y] == -1)  continue;   
        if(index == n){  
        while(!q.empty())  
            q.pop();   
            break;   
        }  
        for(int i = 0; i < 4; i++){  
            int itx = x + movex[i];   
            int ity = y + movey[i];   
            if(range(itx) && range(ity) && dist[itx][ity] != -1 && dist[itx][ity] == INF){  
                node.x = itx;   
                node.y = ity;   
                node.t = t + 1;   
                dist[itx][ity] = t + 1;   
                q.push(node);   
            }  
        }  
    }  
    while(index < n)  
    {  
        dist[a[index].x][a[index].y] = -1;   
        for(int i = 0; i < 4; i++){  
            if(range(a[index].x + movex[i]) && range(a[index].y + movey[i])){
                dist[a[index].x + movex[i]][a[index].y + movey[i]] = -1;
            }   
        }   
        index ++ ;   
    }  
    int ans = INF;   
    for(int i = 0; i < maxn; i++){  
        for(int j = 0; j < maxn; j++){  
            if(dist[i][j] != -1){
                ans = min(ans, dist[i][j]);
            }
        }
    }  
    if(ans != INF)  printf("%d\n",ans);   
    else  printf("-1\n");   
    }  
    return 0;   
}  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值