poj 3669 Meteor Shower(bfs)
来源:互联网 发布:淘宝超低价宝贝集锦 编辑:程序博客网 时间:2024/06/05 11:41
问题描述
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 meteori will striking point (Xi,Yi) (0 ≤Xi ≤ 300; 0 ≤ Yi ≤ 300) at timeTi (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.
输入
* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers:Xi,Yi, and Ti
输出
* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
样例输入
40 0 22 1 21 1 20 3 5
样例输出
5
题目大意就是陨石雨撞地球,你坐上飞机开始逃跑,陨石砸过的点以及周围4个点都不能经过,问你最少时间到安全地方去,即陨石砸不到的地方。
首先把图预处理,先存inf,然后把输入的点以及周围四个点的值都改为陨石落下的时间,然后bfs,bfs的时候要注意:只有这个点为inf或者当前时间+1> 这个点的值才能走,否则就不能走,走过的地方把他改为0,就变相的不能走回头路了。
代码如下:
#include <iostream>#include <cstdio>#include <queue>using namespace std;const int inf=9999999;struct node{ int x,y,t; node(int a,int b,int c) { x=a;y=b;t=c; }};int n,ma[305][305];queue<node>que;int dx[]={1,0,0,-1};int dy[]={0,1,-1,0};int bfs(){ node no1(0,0,0); ma[0][0]=0; que.push(no1); while(!que.empty()) { node no2=que.front(); que.pop(); for(int i=0;i<4;i++) { int xx=no2.x+dx[i]; int yy=no2.y+dy[i]; if(xx<0||yy<0||no2.t+1>=ma[xx][yy]) continue; if(ma[xx][yy]==inf) return no2.t+1; ma[xx][yy]=0; node no3(xx,yy,no2.t+1); que.push(no3); } } return -1;}int main(){ scanf("%d",&n); for(int i=0;i<305;i++) for(int j=0;j<305;j++) ma[i][j]=inf; for(int i=0;i<n;i++) { int x,y,c; scanf("%d %d %d",&x,&y,&c); for(int i=0;i<4;i++) { int xx=x+dx[i]; int yy=y+dy[i]; if(xx>=0&&yy>=0) ma[xx][yy]=min(c,ma[xx][yy]); } ma[x][y]=min(c,ma[x][y]); } printf("%d\n",bfs());}
- poj 3669 Meteor Shower (bfs)
- POJ 3669-Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower BFS
- POJ 3669 Meteor Shower(BFS)
- POJ-3669 Meteor Shower BFS
- Meteor Shower (poj 3669 bfs)
- [bfs] poj 3669 Meteor Shower
- poj 3669 Meteor Shower 【BFS】
- Poj 3669 Meteor Shower【bfs】
- POJ 3669 Meteor Shower (BFS)
- poj 3669 Meteor Shower(bfs)
- POJ 3669:Meteor Shower(bfs)
- [POJ] 3669 Meteor Shower [BFS]
- POJ 3669 Meteor Shower BFS 水~
- POJ 3669 Meteor Shower (BFS)
- POJ 3669 Meteor Shower (BFS + 预处理)
- POJ 3669 Meteor Shower (bfs)
- spring(10)------Bean三种管理方式
- Java 中的 hashcode一
- 服务器创建用户并允许用户远程登录服务器
- Spring4学习(二):创建项目 Hello Spring
- 深入理解java虚拟机(八):java内存分析工具-MAT和OQL
- poj 3669 Meteor Shower(bfs)
- ★hdu1427(经典dfs)
- Android Studio报错
- Java7中Switch支持String
- Android学习笔记之ToggleButton
- 设计模式之策略模式
- Opencv报“HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP”错误处理方法
- CCProgressTimer
- 某公司面试题