题解报告:poj 3669 Meteor Shower(bfs)

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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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: XiYi, 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解法。这道题就是求从原点逃离到安全坐标位置需要花费的最小时间,也就是求最少步数,因为每花费1个单位时间可以看作是走1步。流星落下的那个坐标点周围横竖四个方向也会被炸掉,并且要求每到达一个坐标点所花费的时间都要小于该点被炸毁的时间,因此我们可以将图中每个坐标点被炸的最小时间都初始化为INF,读入的时候顺便更新一下该点及周围横竖四个坐标点被流星炸掉的最小时间,然后用一个结构体记录到达每个坐标点所花费的最小时间,再用队列实现按层遍历即可,具体注释在代码里。
AC代码:
 1 #include<cstdio>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 const int INF=0x3f3f3f3f;
 6 const int maxn=305;
 7 struct node{int x,y,t;}nod;//记录当前到达坐标点的最小时间
 8 int m,xi,yi,ti,mp[maxn][maxn],dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//方向数组
 9 bool vis[maxn][maxn];//表示该坐标点是否已经被访问
10 int bfs(int x,int y){//这里不更新原数组mp,mp数组记录的是每个坐标点被流星砸中的最小时间
11     nod.x=x,nod.y=y,nod.t=0;//先初始化第一个坐标点
12     queue<node> que;
13     memset(vis,false,sizeof(vis));//全部坐标标记为未访问状态
14     que.push(nod);//先将第一个节点入队
15     vis[x][y]=true;//同时将其标记为已访问状态
16     while(!que.empty()){//按层次遍历
17         nod=que.front();que.pop();
18         int tx=nod.x,ty=nod.y,tt=nod.t;
19         if(mp[tx][ty]==INF)return tt;//表示能到达安全的地方,直接返回已成功逃离到安全坐标位置的最少时间
20         for(int i=0;i<4;++i){//遍历横竖四个邻接点
21             nod.x=tx+dir[i][0],nod.y=ty+dir[i][1],nod.t=tt+1;//走到邻接点的时间只需加1
22             if(nod.x>=0&&nod.y>=0&&!vis[nod.x][nod.y]&&nod.t<mp[nod.x][nod.y]){
23     //注意下界不能小于0,没有上界,因此数组应该大小开比300还大一些
24     //如果该点还未访问,并且到达该点的时间必须比在该点被破坏的时间还小,这样才可以逃过灾难也符合题目要求
25                 que.push(nod);//将坐标点nod入队
26                 vis[nod.x][nod.y]=true;//并且标记坐标点nod为已访问状态
27             }
28         }
29     }
30     return -1;//表示不能找到安全的位置,则返回-1
31 }
32 int main(){
33     scanf("%d",&m);
34     memset(mp,0x3f,sizeof(mp));//全部初始化为INF,数组元素用来标记流星砸中该点的最小时间,INF表示该点是安全的
35     while(m--){
36         scanf("%d%d%d",&xi,&yi,&ti);
37         mp[xi][yi]=min(mp[xi][yi],ti);//先更新一下(xi,yi)这一点被流行砸中的最小时间
38         for(int i=0;i<4;++i){//同时更新(xi,yi)这一点横竖4个方向同样被流星砸中的最小时间
39             int nx=xi+dir[i][0],ny=yi+dir[i][1];
40             if(nx>=0&&ny>=0)mp[nx][ny]=min(mp[nx][ny],ti);
41             //注意下界不能小于0,没有上界
42         }
43     }
44     printf("%d
",bfs(0,0));//从坐标原点(0,0)开始进行广度优先搜索
45     return 0;
46 }
原文地址:https://www.cnblogs.com/acgoto/p/9438065.html