#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int dx[4]={+1,0,-1,0};
int dy[4]={0,+1,0,-1};
struct type{
int x,y,t;
}meteor[50000+5],now,next;
int n,map[305][305],latest;
bool vis[305][305];
int bfs()
{
memset(vis,0,sizeof(vis)); //初始化标记数组
queue<type> q;
now.x=0;
now.y=0;
now.t=0;
vis[now.x][now.y]=1;
q.push(now);
while(!q.empty())
{
now=q.front();q.pop();
if(map[now.x][now.y] == 2139062143) return now.t; //如果当前这个格子的被流星摧毁时间是正无穷,那么已经安全
for(int i=0;i<4;i++)
{
next.x=now.x+dx[i] , next.y=now.y+dy[i] , next.t=now.t+1 ;
if(next.x >= 0 && next.y >= 0 && map[next.x][next.y] > next.t && !vis[next.x][next.y])
{
vis[next.x][next.y]=1;
q.push(next);
}
}
}
return -1;
}
int main()
{
while(scanf("%d",&n) != EOF)
{
memset(map,0x7F,sizeof(map)); //先将地图上每个格子初始化为“正无穷”
latest=-1;
for(int i=1;i<=n;i++)
{
scanf("%d %d %d",&meteor[i].x,&meteor[i].y,&meteor[i].t);
if(map[meteor[i].x][meteor[i].y] > meteor[i].t) map[meteor[i].x][meteor[i].y] = meteor[i].t;
for(int j=0;j<4;j++){
next.x=meteor[i].x+dx[j];
next.y=meteor[i].y+dy[j];
if(map[ (next.x) ][ (next.y) ] > meteor[i].t) map[ (next.x) ][ (next.y) ] = meteor[i].t;
} //将地图上的每个格子更新,使其值成为该格最早被流星摧毁的时间
}
printf("%d\n",bfs());
}
}
POJ 3669 - Meteor Shower
最新推荐文章于 2021-09-04 20:28:50 发布