POJ 3669 Meteor Shower(bfs)
来源:互联网 发布:上古卷轴5ece捏脸数据 编辑:程序博客网 时间:2024/05/17 03:37
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<iostream>#include<queue>#include<vector> #include<cstring>#include<cstdio>using namespace std;int t[311][311];int m;struct node{int x,y,step;};int book[311][311];int dire[4][2]={-1,0,1,0,0,-1,0,1};int bfs(){int ans=-1;queue<node>q;q.push(node{0,0,0});book[0][0]=1;while(!q.empty()){node tt=q.front();q.pop();int x=tt.x;int y=tt.y;int step=tt.step;if(t[x][y]==-1){ans=step;break;}for(int i=0;i<4;i++){int nx=x+dire[i][0];int ny=y+dire[i][1];if(nx<0||ny<0)continue;if(!book[nx][ny]&&(step+1<t[nx][ny])||t[nx][ny]==-1){book[nx][ny]=1;q.push(node{nx,ny,step+1});}}}return ans; }int main(){cin>>m;memset(t,-1,sizeof(t));for(int i=1;i<=m;i++){int x,y,tt;scanf("%d%d%d",&x,&y,&tt);if(t[x][y]==-1)t[x][y]=tt;else if(t[x][y]>=0)t[x][y]=min(t[x][y],tt);for(int j=0;j<4;j++){int nx=x+dire[j][0];int ny=y+dire[j][1];if(nx<0||ny<0)continue;if(t[nx][ny]==-1)t[nx][ny]=tt;else if(t[nx][ny]>=0)t[nx][ny]=min(t[nx][ny],tt);}}printf("%d\n",bfs());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)
- 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)
- POJ 3669 Meteor Shower BFS
- 介绍Scanner、Random类
- ios开发 使用系统相册和相机
- sdut2605——A^X mod P
- IMWeb训练营-ToDoList
- SQL缓存的命令 l或list,/或r,n,del n,a,c
- POJ 3669 Meteor Shower(bfs)
- Java集合框架
- PCB布线规则
- 【寒江雪】静态测试之代码分析
- C++抽象编程——递归策略(2)——子集和问题
- ReactNative实现emoji表情图文混排方案
- .....数组操作
- C++远征之封装篇——字符串类型
- 一条过之 一条SQL搞定信息增益的计算