poj 3669 Meteor Shower
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 16339 | Accepted: 4293 |
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
题意: 牛Bessie想要躲避流星雨,于是准备乘飞机逃跑。首先已知有M颗流星,每颗流星的位置(x,y)以及下落的时间t都已知,牛的出发点位于坐标轴原点,现在牛想要跑到一个流星砸不到的点,问至少要花多少时间(牛在出发点的时间记为0,每走一步花1单位的时间)。
思路:首先用一张图来记录每一个点会被流星砸到的最早时间,若某点不会被砸到则可记为无穷大,之后广度优先搜索,对于每一个点,若在图的范围并且当前时间还没被流星砸到且没被访问过,那么访问,之后看一下该点是否安全,安全就退出搜索,否则将该点入队。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int N_MAX = ;
int field[N_MAX][N_MAX];//标记好每个位置被流星砸的时间
bool visited[N_MAX][N_MAX];
int direction[][] = { {,},{-,},{,},{,-},{,} };
int last;
struct MM {
int X, Y, T;
bool operator<(const MM&b)const{
return T < b.T;
}
};
MM m[];
int bfs(const int &x1,const int &y1,const int&cur_T) {
memset(visited, , sizeof(visited));
visited[x1][y1] = true;
queue<MM>que;MM cur;
cur.X = x1;cur.Y = y1, cur.T = cur_T;
que.push(cur);
while (!que.empty()) {
MM p = que.front();que.pop();
for (int i = ;i < ;i++) {
cur = p;
cur.X = direction[i][] + p.X; cur.Y = direction[i][] + p.Y;
cur.T++;
if (cur.X>= && cur.Y>= && field[cur.X][cur.Y]>cur.T&&!visited[cur.X][cur.Y]) {
visited[cur.X][cur.Y] = true;
if (field[cur.X][cur.Y]>last)//说明这一点不会被炸到
return cur.T;
que.push(cur);
}
}
}
return -;
}
int main() {
int M;
scanf("%d",&M);
for (int i = ;i < M;i++)
scanf("%d%d%d",&m[i].X,&m[i].Y,&m[i].T); sort(m,m+M);//按流星砸的时间的先后顺序排
last = m[M - ].T; for (int i = ;i < N_MAX;i++) {
for (int j = ;j < N_MAX;j++)
field[i][j] = INT_MAX;
} for (int i = ;i < M;i++) {
for (int j = ;j < ;j++) {
int x = m[i].X + direction[j][],y=m[i].Y+direction[j][];
if (x >= && y >= && field[x][y]>m[i].T) field[x][y] = m[i].T;
}
} if (field[][] == )
cout << - << endl;
else {
cout << bfs(,,) << endl;
} return ;
}
poj 3669 Meteor Shower的更多相关文章
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- 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 ...
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- POJ 3669 Meteor Shower BFS求最小时间
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31358 Accepted: 8064 De ...
- POJ 3669 Meteor Shower BFS 水~
http://poj.org/problem?id=3669 题目大意: 一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达 ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- BFS搜索:POJ No 3669 Meteor Shower
#include <iostream> #include <cstring> #include <queue> #include <cstdio> #i ...
随机推荐
- 云服务器 ECS Linux 误删除文件恢复方法介绍
云服务器 ECS Linux 下,rm -rf 意味着一旦删除的文件是无法挽回的.但如果在没有文件覆盖操作的前提下,可以先尝试相关方式进行文件恢复. 本文对此进行简要说明. https://help ...
- 获取div相对文档的位置
获取div相对文档的位置,两个方法 经测试 document.getElementById("btn").getBoundingClientRect() 在IE6下有2像素的bug ...
- 如何为Linux安装Go语言
导读 Go 语言又称为 golang, 是由 Google 最初开发的一种开源编程语言,其在设计时就遵循了简单.安全和速度的 3 大原则.Go 语言具有多种调试.测试.分析和代码审查工具,如今 Go ...
- 为rm命令增加回收站功能
rm是个强大的命令,特别是rm -rf有时候强大到让你欲哭无泪,当你想清除当前目录下的所有文件和目录时,很简单 $sudo rm -rf ./* 这没什么,但是,但是如果不小心打成这样 $sudo r ...
- java 删除字符串中的反斜杠\
Java中有时候会打印出来会含有反斜杠(\)的字符串,我们需要删除时,可以使用 replace() 或 replaceAll() 但是要注意的是replaceAll()里面用的是正则表达式,所以一个斜 ...
- 关于消除MySQL输入错误后的警报声
找到mysql安装目录中的 my.ini 配置文件,在[mysql]行的下面加入一行 no-beep 即可消除声音. [mysql] no-beep default-character-set=gb2 ...
- ArcGis学习教程免费版在线观看
ArcGis学习教程免费版在线观看 作者:池建 文章来源:清华大学出版社 点击数:150220 更新时间:2013-8-8 摘要:Arcgis学习视频教程根据书籍章节逐步讲解较为详细 ...
- 【阿里云产品公测】OpenSearch初探
作者:阿里云用户 yqzzzz 这两天在折腾站内搜索,下午照例上阿里云网站看看ECS,OSS情况,恰巧看到免费公测的活动,咦,OpenSearch,看起来这不就是我要找的东西么! 1分钟时间填完申请 ...
- 跨越跳板机传文件nc
从线上服务器与本机互传文件 传输方 nc -l 10000 < a.tar 接收方 nc xx.xx.xx.xx 10000 >a.tar 原理: 文件传输方运行nc,指定端口,设置监听文 ...
- Oracle 约束类型
在Oracle中的约束类型:NOT NULLUNIQUE KeyPRIMARY KEYFOREIGN KEYCHECK create table emp--创建表格 ,注意约束( empno numb ...