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
题解:
这道题要先预处理,先把坐标平面上每一个最早被击打的时间求出来,并放在坐标平面上,然后就可以开始做BFS了,注意建一个vis数组来标记是否走过,如果走回头路的话会tle= =。
#include<iostream>
#include<memory.h>
#include<queue>
using namespace std;
int m[310][310];
bool ori[310][310];
int mem[310][310];//用来记录最开始输入的值 因为在m数组里该点的值可能会被其他点覆盖掉
bool vis[310][310];//防止走回头路 不然会tle
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct node{
int x;
int y;
int time;
};
void bfs()
{
node start;
start.x=0;
start.y=0;
start.time=0;
if(m[0][0]==0)
{
cout<<"-1";
return;
}
queue<node> q;
q.push(start);
vis[0][0]=true;
while(!q.empty())
{
node temp=q.front();
node temp2;
for(int i=0;i<4;i++)
{
temp2.x=temp.x+to[i][0];
temp2.y=temp.y+to[i][1];
temp2.time=temp.time+1;
if(temp2.x>=0 && temp2.x<310 && temp2.y>=0 && temp2.y<310 && !vis[temp2.x][temp2.y])
{
if(m[temp2.x][temp2.y]==-1)
{
cout<<temp2.time<<endl;
return;
}
if(m[temp2.x][temp2.y]>temp2.time)
{
q.push(temp2);
vis[temp2.x][temp2.y]=true;
}
}
}
q.pop();
}
cout<<"-1"<<endl;
}
int main()
{
int M;
cin>>M;
memset(m,-1,sizeof(m));//先把所有的点初始化为-1 -1就是说是安全的点
memset(ori,false,sizeof(ori));//ori来判读该点是不是输入时的点
memset(vis,false,sizeof(vis));
int x,y,t;
while(M--)
{
cin>>x>>y>>t;
m[x][y]=t;
ori[x][y]=true;
mem[x][y]=t;
}
for(int i=0;i<310;i++)
{
for(int j=0;j<310;j++)
{
if(ori[i][j]==true)
{
if(i-1>=0)
{
if(m[i-1][j]>=0)
m[i-1][j]=min(m[i-1][j],mem[i][j]);
else
m[i-1][j]=mem[i][j];
}
if(j-1>=0)
{
if(m[i][j-1]>=0)
m[i][j-1]=min(m[i][j-1],mem[i][j]);
else
m[i][j-1]=mem[i][j];
}
if(i+1<310)
{
if(m[i+1][j]>=0)
m[i+1][j]=min(m[i+1][j],mem[i][j]);
else
m[i+1][j]=mem[i][j];
}
if(j+1<310)
{
if(m[i][j+1]>=0)
m[i][j+1]=min(m[i][j+1],mem[i][j]);
else
m[i][j+1]=mem[i][j];
}
}
}
}//至此初始化完成 不会被轰炸到的点为-1 其他的点是将要被轰炸到的最小值
/*for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
cout<<m[i][j]<<" ";
}
cout<<endl;
}*/
bfs();
return 0;
}