POJ3669Meteor Shower【BFS】
来源:互联网 发布:北京软件培训收费 编辑:程序博客网 时间:2024/05/22 16:41
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
一个人刚开始在原点坐标里第一象限的某些点在某些时间会掉落流星流星会毁灭该点和其相邻的点求人最短多长时间跑到安全地带
#include<cstdio>#include<cstdlib>#include<cstring>#include<queue>using namespace std;int map[500][500];int vis[500][500];int mov[][2]={0,1,0,-1,1,0,-1,0};struct Node{int x;int y;int dist;};int MIN(int a,int b){return a<b?a:b;}bool judge(int x,int y){if(x>=0&&y>=0)return true;return false;}int bfs(int x,int y){int i;memset(vis,0,sizeof(vis));queue<Node>Q;Node u,v;u.x=x;u.y=y;u.dist=0;vis[x][y]=1;Q.push(u);while(!Q.empty()){u=Q.front();Q.pop();if(map[u.x][u.y]==-1)return u.dist;for(i=0;i<4;++i){v.x=u.x+mov[i][0];v.y=u.y+mov[i][1];v.dist=u.dist+1;if(judge(v.x,v.y)&&(v.dist<map[v.x][v.y]||map[v.x][v.y]==-1)&&!vis[v.x][v.y]){vis[v.x][v.y]=1;Q.push(v);}}}return -1;}int n;void input(){int a,b,c;memset(map,-1,sizeof(map));for(int i=0;i<n;++i){scanf("%d%d%d",&a,&b,&c);if(map[a][b]!=-1)map[a][b]=MIN(map[a][b],c);else map[a][b]=c;if(map[a+1][b]!=-1)map[a+1][b]=MIN(map[a+1][b],c);else map[a+1][b]=c;if(a-1>=0){if(map[a-1][b]!=-1)map[a-1][b]=MIN(map[a-1][b],c);else map[a-1][b]=c;}if(map[a][b+1]!=-1)map[a][b+1]=MIN(map[a][b+1],c);else map[a][b+1]=c;if(b-1>=0){if(map[a][b-1]!=-1)map[a][b-1]=MIN(map[a][b-1],c);else map[a][b-1]=c;}}}int main(){scanf("%d",&n);input();printf("%d\n",bfs(0,0));return 0;}
- poj3669Meteor Shower(bfs)
- POJ3669Meteor Shower【BFS】
- POJ3669Meteor Shower(bfs)
- POJ3669Meteor Shower
- poj3669Meteor Shower
- POJ3669 Meteor Shower BFS
- poj3669 Meteor Shower BFS
- POJ3669 Meteor Shower(BFS)
- POJ3669 Meteor Shower 【BFS】
- poj_3669_Meteor Shower(BFS+预处理)
- POJ_3669 Meteor Shower(BFS)
- POJ3669-Meteor Shower-BFS
- poj3669 Meteor Shower bfs
- Poj3669 meteor shower bfs
- POJ3669 Meteor Shower (BFS)
- Meteor Shower (bfs)
- Meteor Shower (bfs)
- poj 3669 Meteor Shower (bfs)
- 解决VMware关闭后,系统卡顿问题
- c#网络编程资料
- Curling 2.0 POJ 3009
- C++学习之:打开文件对话框和打开文件夹对话框
- 互联网社会建设
- POJ3669Meteor Shower【BFS】
- **2014年毕业至今,这一年来的风风雨雨**
- 顺序表
- hdu5355 思维+爆搜
- http://blog.csdn.net/vichie2008/article/details/40823531
- STL——空间配置器
- 《基于BootStrap3的JSP项目实例教程》第1讲
- 【NOIP2013模拟10.23】君と彼女の恋
- 元组 列表 集合 字典 python