Meteor Shower POJ - 3669 (bfs+优先队列)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26455 | Accepted: 6856 |
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
4
0 0 2
2 1 2
1 1 2
0 3 5
Sample Output
5
题意:流星来袭,在一个方格中,贝茜在(0,0)点,为了防止被流星击中,贝茜需要移动到安全的位置。一共有n颗流星,给出流星落下的坐标(xi,yi)和时间ti,每一颗流星落地时上下左右的格子也为遭受毁灭。贝茜经过的路程是不能被毁灭的。
题解:首先要建图,用map[][]数组储存好每个点遭受流星打击的时间,不受流星打击的用-1涵盖。(不能用0覆盖,部分点可能在0时刻遭受打击) 然后遍历整张图,找到不受流星打击的点即可,因为要求最短时间,所有要用到优先队列,优先时间最小的点出队列。根据题意我们知道在图内的不受流星打击的点和没有经过流星打击的点能进入队列。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=; int dx[]={,,-,},dy[]={,,,-};
int maze[maxn][maxn];
int vis[maxn][maxn]; struct node
{
int x,y,step;
}temp,a;
bool operator < (const node &a,const node &b)
{
return a.step>b.step;
} int min(int a,int b)
{
return a>b?b:a;
}
void bfs()
{
int i;
memset(vis,,sizeof(vis));
priority_queue<node>q;
a.x=; a.y=;
a.step=;
q.push(a);
vis[][]=;
while(!q.empty())
{
a=q.top();
q.pop();
if(maze[a.x][a.y]==-)
{
cout<<a.step<<endl;
return ;
}
for(i=;i<;i++)
{
temp.x=a.x+dx[i];
temp.y=a.y+dy[i];
temp.step=a.step+;
if(temp.x>= && temp.y>= && (maze[temp.x][temp.y]==- || maze[temp.x][temp.y]>temp.step))
{
if(!vis[temp.x][temp.y])
q.push(temp);
vis[temp.x][temp.y]=;
}
} }
cout<<-<<endl; } int main()
{
int n,x,y,t;
memset(maze,-,sizeof(maze));
cin>>n;
for(int i=;i<n;i++)
{
cin>>x>>y>>t;
if(maze[x][y]==-)
maze[x][y]=t;
else
maze[x][y]=min(maze[x][y],t);
for(int j=;j<;j++)
{
int nx=x+dx[j];
int ny=y+dy[j];
if(nx>= && ny>=)
{
if(maze[nx][ny]==-)
maze[nx][ny]=t;
else
maze[nx][ny]=min(t,maze[nx][ny]);
}
} }
bfs();
return ;
}
Meteor Shower POJ - 3669 (bfs+优先队列)的更多相关文章
- BFS:Meteor Shower(POJ 3669)
奔跑吧,傻牛 题目大意:这只Bessie的牛又要来闹事了,这次她的任务就是来躲流星雨,流星雨就是在一定时间会从天上砸到Bessie的所在的正方形区域内(Bessie在0,0的位置),然后砸下 ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- poj 3635(bfs+优先队列)
题目链接:http://poj.org/problem?id=3635 思路:本题主要运用的还是贪心思想,由于要求st->ed的最小花费,那么每经过一个城市,能不加油就尽量不加油,用dp[i][ ...
- poj3669 Meteor Shower(预处理+bfs)
https://vjudge.net/problem/POJ-3669 先给地图a[][]预处理每个位置被砸的最小时间.然后再bfs. 纯bfs,还被cin卡了下时间.. #include<io ...
- 【BZOJ】1611: [Usaco2008 Feb]Meteor Shower流星雨(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1611 一眼题,bfs. #include <cstdio> #include <c ...
- bzoj 1611: [Usaco2008 Feb]Meteor Shower流星雨【BFS】
t记录每个格子最早被砸的时间,bfs(x,y,t)表示当前状态为(x,y)格子,时间为t.因为bfs,所以先搜到的t一定小于后搜到的,所以一个格子搜一次就行 #include<iostream& ...
- poj 3669 bfs(这道题隐藏着一个大坑)
题意 在x,y坐标系,有流星会落下来,给出每颗流星落下来的坐标和时间,问你能否从(0,0)这个点到一个安全的位置.所谓的安全位置就是不会有流星落下的位置. 题解: 广搜,但是这里有一个深坑,就是搜索的 ...
- POJ 3669 Meteor Shower【BFS】
POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
随机推荐
- NET Core中使用Redis和Memcached
.NET Core中使用Redis和Memcached的序列化问题 前言 在使用分布式缓存的时候,都不可避免的要做这样一步操作,将数据序列化后再存储到缓存中去. 序列化这一操作,或许是显式的,或许 ...
- Get和Post的初步探究
Get请求和Post请求这两种基本请求类型,大部分开发者心里大概都有所谓的"标准答案",但博主最近用Postman测试接口的时候,遇到传参的问题:用post请求,参数放在reque ...
- 多线程串口通信 MFC CSerialPort
写在前面: 晚上应该继续完成未写完的代码,但Chrome上打开的标签实在太多了,约30个了,必须关掉一些,所以需要把自己看的整理一下然后关掉.本次主要写点MFC环境下多线程串口通信相关的东西,这包括线 ...
- 编写SQL语句操作数据库(慕课SQLite笔记)
安卓常用数据存储方式之一SQLite学习及操作笔记 0.视频地址:http://www.imooc.com/video/3382 1.每个程序都有自己的数据库 默认情况下是各自互不干扰 1)创建一个数 ...
- Linux下安装并配置SSH服务
一.使用命令检测Linux系统上是否已经安装了SSH服务:(命令:rpm -qa |grep ssh) 二.如果没有安装SSH软件包,可以通过yum 或rpm安装包进行安装(命令:yum instal ...
- js构造方法
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Java ...
- 使用脚本在Linux服务器上自动安装Kubernetes的包管理器Helm
Helm之于Kubernetes好比yum之于Red Hat Enterprise Linux,或者apt-get之于Ubuntu. Helm是由helm CLI和Tiller组成,是典型的Clien ...
- 人脸识别 python调用face++ 功能测试
使用python调用face++的API,调用detect功能,识别人脸 首先进入face++官网注册,获得API Key和API Secret.使用官网提供的免费python api调用功能,提供了 ...
- 校招准备-关系型数据库与nosql
深入理解常见的数据库的设计架构, 其中用到的数据结构, 算法等 SQL执行流程和优化, 可以了解一下calcite: https://calcite.apache.org/
- Yum简单使用小结
Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动 ...