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
题意:输入n,其实是流星落下,并且流星落下会破坏和它相邻的四个方向的点,我用爆炸点代替,接下来的n行输入炸弹点的坐标(x,y)和该炸弹爆炸的时间,并且该点爆炸,和它相邻的上下左右四个点也会被炸毁,求离开炸弹区域的最短时间。
注意:当爆炸点为(0,0),并且爆炸时间为0时,输出-1,continue。
用二维数组time[ ][ ]存储(x,y)点炸弹爆炸的最小时间,mp[ ][ ]数组标记(x,y)点是否爆炸,用book[ ][ ] 数组标记该点是否已经走过。每一次输入爆炸点的坐标,都要将和它相邻的四个方向标记为爆炸点,并且更新该点和与它相邻的四个方向的点的爆炸时间,得到每一个爆炸点的最短时间。
用队列求到达安全区域的最短时间,若该点的爆炸时间大于当前的时间,并且该点未被标记过,且未出界,该点入队,时间加1。
若找到安全点,输出当前时间。若队列为空时,还未找到安全点输出-1。
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int m,f;
int mp[320][320],time[320][320],book[320][320];
int nx[4][2]= {0,-1,-1,0,0,1,1,0};
struct note
{
int x,y,s;
};
void play(int x,int y,int z)
{
for(int i=0; i<4; i++)
{
int tx=nx[i][0]+x;
int ty=nx[i][1]+y;
if(tx>=0&&ty>=0)
{
mp[tx][ty]=-1;
time[tx][ty]=min(time[tx][ty],z);
}
}
}
void bfs()
{
note now,tmp;
now.x=0;
now.y=0;
now.s=0;
book[0][0]=1;
queue<note> Q;
Q.push(now);
while(!Q.empty())
{
now=Q.front();
Q.pop();
if(mp[now.x][now.y]==0)
{
f=1;
printf("%d\n",now.s);
return ;
}
for(int i=0; i<4; i++)
{
tmp.x=now.x+nx[i][0];
tmp.y=now.y+nx[i][1];
tmp.s=now.s+1;
if(tmp.x>=0&&tmp.y>=0&&!book[tmp.x][tmp.y])
{
if(mp[tmp.x][tmp.y]==-1)
{
if(time[tmp.x][tmp.y]>tmp.s) //如果该点在目前时刻未被炸毁,入队
{
book[tmp.x][tmp.y]=1;
Q.push(tmp);
}
}
else
{
book[tmp.x][tmp.y]=1;
Q.push(tmp);
}
}
}
}
}
int main()
{
int a,b,c;
while(~scanf("%d",&m))
{
memset(mp,0,sizeof(mp));
memset(time,INF,sizeof(time));
memset(book,0,sizeof(book));
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
mp[a][b]=-1;
time[a][b]=min(time[a][b],c); //这里一定要更新为最小值
play(a,b,c);
}
if(time[0][0]==0 && mp[0][0]==-1)
{
printf("-1\n");
continue;
}
f=0;
bfs();
if(!f)
printf("-1\n");
}
return 0;
}