Meteor Shower
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16508 | Accepted: 4331 |
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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int INF=100000000;
const int maxn=400;
int a[maxn][maxn]={INF};
int visit[maxn][maxn]={0};
int run[maxn][maxn]={INF};
int f[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int mstep=0;
typedef pair <int,int> p;
int bfs(int i,int j){
queue<p> que;
run [i][j]=0;
que.push(p(i,j));
while(que.size()){
int ni,nj;
p s=que.front();que.pop();
visit[s.first][s.second]=1;
for(int z=0;z<4;z++){
ni=s.first+f[z][0];
nj=s.second+f[z][1];
if(ni>=0&&nj>=0&&a[ni][nj]>a[s.first][s.second]+1&&visit[ni][nj]==0) {
run[ni][nj]=min(run[s.first][s.second]+1,run[ni][nj]);
if(a[ni][nj]==INF) {
mstep=run[ni][nj];
break;
}
else{
que.push(p(ni,nj));
}
}
}
if(mstep!=0)
break;
}
return mstep;
}
int main()
{
int b,x,y,z;
scanf("%d",&b);
while(b--){
scanf("%d %d %d",&x,&y,&z);
a[x][y]=z;
}
printf("%d",bfs(0,0));
return 0;
}
run[maxn][maxn]用来保存走的时间, a[maxn][maxn]用来记录有流星的点及砸中时的时间,INF代表是安全点,由于是 bfs()搜索,不能再走走过的点,用 visit[maxn][maxn] 来记录有没有走过
欢迎各位大佬指正