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 ...
随机推荐
- 安装Jaspersoft Studio
下载位置:http://community.jaspersoft.com/project/jaspersoft-studio/releases.
- IO流----File,递归,字节流,字符流
要把数据持久化存储,就需要把内存中的数据存储到内存以外的其他持久化设备(硬盘.光盘.U盘等)上. 当需要把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作. 当把持久设备上的数据读 ...
- BZOJ4939: [Ynoi2016]掉进兔子洞(莫队 bitset)
题意 题目链接 一个长为 n 的序列 a. 有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的个数和,询问独立. 注意这里删掉指的是一个一个删,不是把等于 ...
- Android tess_two Android图片文字识别
文字识别一般都用的tesseract-ocr. GitHub:https://github.com/tesseract-ocr/tesseract 而Android对应的比较推荐的有个tess-two ...
- ios 设置导航栏背景色
//设置导航栏背景色 如果上面的不好用 就用下面的 [self.navigationController.navigationBar setBackgroundImage:[UIImage image ...
- Python之时间表示
Python的time模块中提供了丰富的关于时间操作方法,可以利用这些方法来完成这个需求. time.time() :获取当前时间戳 time.ctime(): 当前时间的字符串形式 time.loc ...
- 【LeetCode】4.Median of Two Sorted Arrays 两个有序数组中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 打开某exe提示"应用程序无法启动,因为应用程序的并行配置不正确……"的解决方案
本人在新安装好了的windows server 2008 r2 (64位)上运行“RefilesName V2.0(文件批量改名).exe”,结果提示: 应用程序无法启动,因为应用程序的并行配置不正确 ...
- sublime text 3的使用过程记录
1.使用cmd命令行: 首先配置环境变量(即将sublime text3.exe所在目录放在path路径).完成之后,既可以利用subl命令行 subl file :: 使用Sublime Text打 ...
- Oracle RAC/Clusterware 多种心跳heartbeat机制介绍 RAC超时机制分析
ORACLE RAC中最主要存在2种clusterware集群件心跳 & RAC超时机制分析: 1.Network Heartbeat 网络心跳 每秒发生一次: 10.2.0.4以后网络心跳 ...