POJ 3669 Meteor shower 简单BFS, 有坑点

                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 (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

题意:一个2b要在下流星雨的地方玩大冒险。

数组maze记录该点不能走的时间。注意:初始化的时候要初始化为-1,或inf

坑点写在注释了。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<queue>
  5
  6 using namespace std;
  7
  8 const int maxn=310;
  9
 10 int maze[maxn][maxn];
 11 bool vis[maxn][maxn];
 12
 13 struct Node
 14 {
 15     int x,y,time;
 16 };
 17
 18 int dx[4]={0,0,-1,1};
 19 int dy[4]={1,-1,0,0};
 20
 21 void initMaze(int m)
 22 {
 23     memset(maze,-1,sizeof(maze));
 24
 25     int u,v,w;
 26
 27     for(int i=1;i<=m;i++)
 28     {
 29         scanf("%d %d %d",&u,&v,&w);
 30         if(maze[u][v]==-1||maze[u][v]>w)
 31         {
 32             maze[u][v]=w;
 33         }
 34         for(int j=0;j<4;j++)
 35         {
 36             int du=u+dx[j];
 37             int dv=v+dy[j];
 38             if(du<0||du>305||dv<0||dv>305)
 39                 continue;
 40             if(maze[du][dv]==-1||maze[du][dv]>w)
 41                 maze[du][dv]=w;
 42         }
 43
 44     }
 45 }
 46
 47 int bfs()
 48 {
 49     memset(vis,false,sizeof(vis));
 50
 51     Node start;
 52     start.x=start.y=start.time=0;
 53
 54
 55     //因为这个地方wa了好几次,如果maze[0][0]=-1可以走的,
 56     //这里却直接输出-1
 57     /*
 58     if(maze[0][0]<=1)
 59         return -1;
 60     */
 61
 62     queue<Node>que;
 63     while(!que.empty())
 64         que.pop();
 65
 66
 67     que.push(start);
 68
 69     vis[0][0]=true;
 70
 71     while(!que.empty())
 72     {
 73         Node cur=que.front();
 74         que.pop();
 75
 76         if(maze[cur.x][cur.y]==-1)
 77             return cur.time;
 78
 79         for(int i=0;i<4;i++)
 80         {
 81             Node cnt;
 82             cnt.x=cur.x+dx[i];
 83             cnt.y=cur.y+dy[i];
 84             cnt.time=cur.time+1;
 85
 86             //刚开始这次305是写成300的
 87             //其实他是可以逃到301,302...的
 88             //所以如果不把这些点放入队列的话,
 89             //可能就找不到安全的地方了,也就错了
 90
 91             if(cnt.x<0||cnt.x>305||cnt.y<0||cnt.y>305)
 92                 continue;
 93
 94             if(vis[cnt.x][cnt.y])
 95                 continue;
 96             if(maze[cnt.x][cnt.y]!=-1&&maze[cnt.x][cnt.y]<=cnt.time)
 97                 continue;
 98             que.push(cnt);
 99             vis[cnt.x][cnt.y]=true;
100         }
101     }
102     return -1;
103 }
104
105 int main()
106 {
107     int m;
108     while(scanf("%d",&m)!=EOF)
109     {
110         initMaze(m);
111
112         int ans=bfs();
113
114         printf("%d\n",ans);
115
116         //debug用
117         /*
118         for(int i=4;i>=0;i--)
119         {
120             for(int j=0;j<5;j++)
121                 printf("%d ",maze[i][j]);
122             printf("\n");
123         }
124         */
125     }
126
127     return 0;
128 }

				
时间: 2024-10-13 02:19:35

POJ 3669 Meteor shower 简单BFS, 有坑点的相关文章

POJ 3669 Meteor Shower【BFS】

POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最早被炸毁的时间 #include <iostream> #include <algorithm> #include <queue> #include <cstring> using namespace std; #define INDEX_MAX 512 int

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 destroy

POJ 3669 Meteor Shower (BFS + 预处理)

Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9677   Accepted: 2718 Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hi

题解报告: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 destroy

POJ 3669 Meteor Shower(流星雨)

POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K 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 sa

【POJ 3669 Meteor Shower】简单BFS

流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封锁).安全地带为永远不会被封锁的点. 简单bfs,开始WA在把平面空间上限当成300*300,但根据题目,这只是有流星雨撞击的范围.实际可走的空间理论上没上限,但分析可得,离原点最近的安全地带一定在(302,302)范围内,所以应可把数组至少开为303*303. 后来WA在把G[0][0]==1的情

poj 3669 Meteor Shower

Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16339   Accepted: 4293 Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they h

POJ - 3669 Meteor Shower(bfs)

题意:某人在时刻0从原点出发,在第一象限范围内移动.已知每个炸弹爆炸的地点和时刻,炸弹爆炸可毁坏该点和它上下左右的点.不能经过已毁坏的点,也不能在某点毁坏的时候位于该点.不会被炸弹毁坏的地方是安全之处,问某人到达安全之处的最短时间. 分析:bfs即可.注意:1.某点多次爆炸或受爆炸点影响而毁坏,取最早时间为该点的毁坏时间.2.bfs走过的点不能再走,注意标记. #pragma comment(linker, "/STACK:102400000, 102400000") #include

[水+bfs] poj 3669 Meteor Shower

题意: 一个迷宫内有有M个炸弹,分别在(xi,yi) 处并在ti时刻爆炸,爆炸包括本身和四周一个单位. 也就是说爆炸完点就不能走了. 然后人在(0,0)点,为走多少步能到达一个永远安全的点,否则输出-1. 思路: 首先预处理所有炸弹,标记每个点在哪个时刻之前是可以通过的. 然后人走一遍bfs就ok了. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cm