POJ 3669 Meteor Shower 普通BFS
来源:互联网 发布:天津网络微信投票公司 编辑:程序博客网 时间:2024/06/07 05:56
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
[Submit] [Go Back] [Status] [Discuss]
传送门:http://poj.org/problem?id=3669
思路:直接BFS找路,开一个数组time[ ][ ]保存坐标爆炸时间,初始化 -1(存在0时刻爆炸);
AC代码:
#include<cstdio>#include<queue>#include<cstring>using namespace std;const int maxn=300+50;struct point{ int x,y;};int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};int step[maxn][maxn];int time[maxn][maxn];int vis[maxn][maxn];int bfs(point& st){ queue<point> q; q.push(st); while(!q.empty()){ st=q.front(); q.pop(); if(time[st.x][st.y]==-1) return 1; for(int i=0;i<4;i++){ point a; a.x=st.x+dx[i]; a.y=st.y+dy[i]; if(a.x>=0&&a.y>=0){ step[a.x][a.y]=step[st.x][st.y]+1; if(!vis[a.x][a.y]&&(step[a.x][a.y]<time[a.x][a.y]||time[a.x][a.y]==-1)){ vis[a.x][a.y]=1; //printf("(%d, %d)---",a.x,a.y); // printf("%d----%d\n",step[a.x][a.y],time[a.x][a.y]); q.push(a); } } } } return 0;}int main(){ int m; while(scanf("%d",&m)!=EOF){ if(m<1) break; memset(time,-1,sizeof(time)); memset(step,0,sizeof(step)); memset(vis,0,sizeof(vis)); for(int i=0;i<m;i++){ int x,y,t; scanf("%d %d %d",&x,&y,&t); if(time[x][y]!=-1) time[x][y]=min(time[x][y],t); else time[x][y]=t; for(int i=0;i<4;i++){ int x1=x+dx[i]; int y1=y+dy[i]; if(x1>=0&&y1>=0) { if(time[x1][y1]!=-1) time[x1][y1]=min(time[x1][y1],t); else time[x1][y1]=t; } } } point st; st.x=0; st.y=0; step[0][0]=0; if(bfs(st)) printf("%d\n",step[st.x][st.y]); else printf("-1\n"); } return 0;}
- 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
- 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 + 预处理)
- 这么长的存储区过程让我改,我都不知道每个表对应是干啥的,字段也没有说明。真的要崩溃了。。。
- 在linux上面安装python3
- CCF 100002. 取数游戏
- avocado接口调用
- 人生感悟
- POJ 3669 Meteor Shower 普通BFS
- 直接修改别人jar包里面的class文件 工具:jclasslib
- c语言位运算
- Javascript数据类型与基本运算
- spring boot 教程(三)配置详解
- java8 ArrayList源码阅读
- arraylist采用for循环删除删不干净问题
- 阿里云服务优惠分享
- Mysql -- Oracle -- Sql server中默认事务隔离级别