POJ3669 Meteor Shower (BFS)
来源:互联网 发布:在淘宝当主播怎么赚钱 编辑:程序博客网 时间:2024/05/22 17:34
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
40 0 22 1 21 1 20 3 5
Sample Output
5
Source
思路:首先应该标记所有会被流星击中的点,其次标记被流星波及到的点的坐标,然后进行BFS搜索,直到有点的坐标满足初始条件即可说明该点是安全的。
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;const int MAX_N = 305;const int MAX_M = 50005;int X[MAX_M],Y[MAX_M],T[MAX_M];int maze[MAX_N][MAX_N],dis[MAX_N][MAX_N];int m,dir[4][2] = {-1,0,0,-1,1,0,0,1};typedef pair<int, int> P;int bfs(){queue<P> q;dis[0][0] = 0;q.push(P(0,0));if(maze[0][0] == 0)//在起点直接就被砸中,则直接over return -1;while(!q.empty()){P p = q.front();q.pop();int x = p.first, y = p.second;if(maze[x][y] == INF)//当前坐标处于安全区域 return dis[x][y];for(int i = 0; i < 4; i ++){//判断相邻的四个方向 int sx = x + dir[i][0], sy = y + dir[i][1];//没越界并且该点没有被访问过并且该点在下一秒或者以前都没有被流星击中过 if(sx >= 0 && sy >= 0 && dis[sx][sy] == INF && dis[x][y] + 1 < maze[sx][sy]) q.push(P(sx,sy)), dis[sx][sy] = dis[x][y] + 1;}}return -1;}void solve(){for(int i = 0; i < MAX_N; i ++)//初始化maze[][]以及dis[][] fill(maze[i],maze[i]+MAX_N,INF),fill(dis[i],dis[i]+MAX_N,INF); for(int i = 0; i < m; i ++){maze[X[i]][Y[i]] = min(maze[X[i]][Y[i]],T[i]);//选择较早的击中时间 for(int j = 0; j < 4; j ++){int sx = X[i] + dir[j][0],sy = Y[i] + dir[j][1];if(sx >= 0 && sy >= 0)//被流星集中点的相邻四点,仍然选择最早的爆炸时间 maze[sx][sy] = min(maze[sx][sy],T[i]); }}int ans = bfs();printf("%d\n",ans);}int main(){while(~scanf("%d",&m)){for(int i = 0; i < m; i ++){scanf("%d%d%d",&X[i],&Y[i],&T[i]);}solve();}return 0;}
- POJ3669 Meteor Shower BFS
- poj3669 Meteor Shower BFS
- POJ3669 Meteor Shower(BFS)
- POJ3669 Meteor Shower 【BFS】
- POJ3669-Meteor Shower-BFS
- poj3669 Meteor Shower bfs
- Poj3669 meteor shower bfs
- POJ3669 Meteor Shower (BFS)
- POJ3669 Meteor Shower(bfs)
- POJ3669-Meteor Shower(BFS)
- poj3669 Meteor Shower(bfs java)
- 宽度优先搜索(BFS):POJ3669--Meteor Shower
- POJ3669——Meteor Shower(BFS)
- POJ3669-Meteor Shower
- POJ3669 Meteor Shower
- 【poj3669】Meteor Shower 搜索
- poj3669-Meteor Shower
- POJ3669 Meteor Shower
- 南师附中集训 七日游(划掉)总结
- android控件12--ScrollView
- npm link的基本用法及实例
- Java实现MySQL图片存取
- 设计模式之六大原则
- POJ3669 Meteor Shower (BFS)
- 高中OJ3505. 【NOIP2013模拟11.4A组】积木(brick)
- android studio常用控件
- 3.机器学习概论(1)--统计学习三大要素
- Services服务
- 关于MySQL中存储时间格式的问题
- UVA 156 Ananagrams(map、set)
- iOS
- @Autowired与@Resource的区别