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
题目的大致意思:
1.玩家从原点(0,0)出发
2.有流星在一定时间里往地图上砸。砸到以后,这个点以及上下左右共5个点就再也不能行走了(砸以前可以走)
3.玩家每个单位时间必须要上下左右走一格,只能在第一象限中移动
4.找到最短的行走距离,保证这个点是安全的,即所有流星都不会砸到这个点
#include<iostream>
#include<cstdio>
using namespace std;
const int M=50100,N=1001,INF=1000000000;
const int mv[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int map[N][N],get[N][N],a[N*N],b[N*N];
bool in(int x,int y)
{
return x>=0&&y>=0&&x<N&&y<N;
}
int main()
{
int m;
scanf("%d",&m);
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
map[i][j]=get[i][j]=INF;
for(int k=1;k<=m;k++)
{
int x,y,t;
scanf("%d%d%d",&x,&y,&t);
if(map[x][y]>t)map[x][y]=t;
for(int i=0;i<4;i++)
if(map[x+mv[i][0]][y+mv[i][1]]>t)
map[x+mv[i][0]][y+mv[i][1]]=t;
}
int u=0,d=0;
a[++u]=0;
b[u]=0;
get[0][0]=0;
int ans,flag=0;
while(u>d)
{
int nx=a[++d],ny=b[d];
if(map[nx][ny]<=get[nx][ny])continue;
if(map[nx][ny]==INF){flag=1;ans=get[nx][ny];break;}
for(int i=0;i<4;i++)
{
int tx=nx+mv[i][0],ty=ny+mv[i][1];
if(!in(tx,ty))continue;
if(get[tx][ty]>get[nx][ny]+1 && map[tx][ty]>get[nx][ny]+1)
{
get[tx][ty]=get[nx][ny]+1;
a[++u]=tx;
b[u]=ty;
}
}
}
if(!flag)ans=-1;
printf("%d\n",ans);
return 0;
}