POJ3669Meteor Shower【BFS】
2015-08-12 21:23
489 查看
Language: Default Meteor Shower
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 4 0 0 2 2 1 2 1 1 2 0 3 5 Sample Output 5 Source USACO 2008 February Silver |
#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; }
相关文章推荐
- 表示数字
- 分页分段分区内存管理
- signal(SIGPIPE, SIG_IGN)
- 菜鸟Dump Memory python 脚本
- 链表操作
- dynamic-- web的创建
- 【玩转GridView】之简单实现隐藏列
- HDU 2680 Choose the best route(dijkstra+优先队列优化)
- 解析JSON数据,并从其中的图片地址取图片
- 黑马程序员--java基础--IO流(一)
- 修改myEclipse的默认的工作空间
- DOM-文档对象模型
- POJ 1113 Wall
- java设计模式----策略模式
- 小脚本标签的应用
- 文件切割和文件合并
- sunburnt 学习笔记(六)添加文档
- UI11_快捷菜单
- leetcode 查找
- 看别人怎么学技术,并记录自己的发展