|
Meteor Shower
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 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 |
[Submit] [Go Back] [Status] [Discuss]
传送门:http://poj.org/problem?id=3669
思路:直接BFS找路,开一个数组time[ ][ ]保存坐标爆炸时间,初始化 -1(存在0时刻爆炸);
AC代码:
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=300+50;
struct point{
int x,y;
};
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int step[maxn][maxn];
int time[maxn][maxn];
int vis[maxn][maxn];
int bfs(point& st){
queue<point> q;
q.push(st);
while(!q.empty()){
st=q.front(); q.pop();
if(time[st.x][st.y]==-1) return 1;
for(int i=0;i<4;i++){
point a;
a.x=st.x+dx[i];
a.y=st.y+dy[i];
if(a.x>=0&&a.y>=0){
step[a.x][a.y]=step[st.x][st.y]+1;
if(!vis[a.x][a.y]&&(step[a.x][a.y]<time[a.x][a.y]||time[a.x][a.y]==-1)){
vis[a.x][a.y]=1;
//printf("(%d, %d)---",a.x,a.y);
// printf("%d----%d\n",step[a.x][a.y],time[a.x][a.y]);
q.push(a);
}
}
}
}
return 0;
}
int main(){
int m;
while(scanf("%d",&m)!=EOF){
if(m<1) break;
memset(time,-1,sizeof(time));
memset(step,0,sizeof(step));
memset(vis,0,sizeof(vis));
for(int i=0;i<m;i++){
int x,y,t;
scanf("%d %d %d",&x,&y,&t);
if(time[x][y]!=-1) time[x][y]=min(time[x][y],t);
else time[x][y]=t;
for(int i=0;i<4;i++){
int x1=x+dx[i];
int y1=y+dy[i];
if(x1>=0&&y1>=0) {
if(time[x1][y1]!=-1) time[x1][y1]=min(time[x1][y1],t);
else time[x1][y1]=t;
}
}
}
point st;
st.x=0; st.y=0;
step[0][0]=0;
if(bfs(st)) printf("%d\n",step[st.x][st.y]);
else printf("-1\n");
}
return 0;
}
面对即将到来的流星雨威胁,Bessie需要找到一条安全的路径逃离原地。此问题通过广度优先搜索(BFS)算法解决,寻找在流星撞击前到达安全地点所需的最短时间。
372

被折叠的 条评论
为什么被折叠?



