一、题目

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 (XiYi) (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: XiYi, 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

二、思路

  • 安全区可以到301
  • 同一个点可以炸两次
  • 必须在第一象限
  • t可以等于0
  • 可以一开始就死,也可以一开始就是安全区

二、思路&心得

  • 要注意数据的范围
  • 广搜常用来求解某个问题的最小值,求解过程中,利用dist数组更新最小值

三、代码


int dist[MAX_SIZE][MAX_SIZE]; int dirction[5][2] = {0, -1, -1, 0, 0, 1, 1, 0, 0, 0}; typedef pair<int, int> P; struct Meteor {
int X;
int Y;
int T;
} meteors[MAX_M]; bool cmp (Meteor a, Meteor b) {
if (a.T < b.T) return true;
else return false;
} bool isLegal(int x, int y) {
if (x >= 0 && x < MAX_SIZE && y >= 0 && y < MAX_SIZE) return true;
else return false;
} int bfs() {
queue<P> que;
if (map[0][0] == 0) return -1;
else if (map[0][0] == MAX_TIME) return 0;
que.push(P(0, 0));
while (que.size()) {
P p = que.front();
que.pop();
int px = p.first, py = p.second;
if (px >= MAX_SIZE || py >= MAX_SIZE) continue;
for(int i = 0; i < 4; i ++) {
int tx = px + dirction[i][0], ty = py + dirction[i][1];
if (isLegal(tx, ty)) {
if (map[tx][ty] == MAX_TIME) {
return ++ dist[px][py];
}
if (dist[px][py] + 1 < map[tx][ty] && !dist[tx][ty]) {
que.push(P(tx, ty));
dist[tx][ty] = dist[px][py] + 1;
}
}
}
}
return -1;
} void solve() {
for (int i = 0; i < MAX_SIZE; i ++) {
for (int j = 0; j < MAX_SIZE; j ++) {
map[i][j] = MAX_TIME;
}
}
for (int i = 0; i < M; i ++) {
scanf("%d %d %d", &meteors[i].X, &meteors[i].Y, &meteors[i].T);
}
sort(meteors, meteors + M, cmp);
for (int i = 0; i < M; i ++) {
for (int j = 0; j < 5; j ++) {
int tx = meteors[i].X, ty = meteors[i].Y;
tx += dirction[j][0];
ty += dirction[j][1];
if (isLegal(tx, ty) && map[tx][ty] > meteors[i].T) {
map[tx][ty] = meteors[i].T;
}
}
}
printf("%d\n", bfs());
} int main() {
while (~scanf("%d", &M)) {
solve();
}
return 0;
}

【搜索】POJ-3669 BFS的更多相关文章

  1. poj 3669 bfs(这道题隐藏着一个大坑)

    题意 在x,y坐标系,有流星会落下来,给出每颗流星落下来的坐标和时间,问你能否从(0,0)这个点到一个安全的位置.所谓的安全位置就是不会有流星落下的位置. 题解: 广搜,但是这里有一个深坑,就是搜索的 ...

  2. POJ 3669 Meteor Shower【BFS】

    POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...

  3. POJ 3669 广度优先搜索

    题意:巨大流星雨即将袭来.每个流星会对击中的地方以及周围(上下左右四格)造成破坏.Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方(在第一象限内).已知每移动一格需要1个时间单 ...

  4. POJ 3669 Meteor Shower(流星雨)

    POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears ...

  5. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  6. POJ 1475 Pushing Boxes 搜索- 两重BFS

    题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...

  7. poj 3249(bfs+dp或者记忆化搜索)

    题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...

  8. POJ 3669 Meteor Shower BFS 水~

    http://poj.org/problem?id=3669 题目大意: 一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达 ...

  9. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  10. POJ 3669 Meteor Shower (BFS+预处理)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

随机推荐

  1. mysql/mariadb学习过程中出现的问题与解决

    mysql> insert into student values(,,'CS'); ERROR (): Duplicate entry ' for key 'PRIMARY'(duplecat ...

  2. 三星S5-PV210内存初始化

    一.S5PV210时钟系统 时钟:一定频率的电信号.   时钟系统:基于CMOS工艺的高性能处理器时钟系统,集成PLL可以从内部触发,比从外部触发更快且更准确,能有效地避免一些与信号完整性相关的问题. ...

  3. 筑基期—C语言

    1.1 环境: 在ANSIC的任何一种是实现中,存在两种不同的环境.第一种是翻译环境,第二种是执行环境.标准明确说明这两种环境不必在同一台机器上,交叉编译器就是在一台机器上运行,但它所产生的可执行代码 ...

  4. C++引用的用处

    原文:http://www.cnblogs.com/ddx-deng/archive/2012/12/16/3755864.html 一.什么是“引用”?声明和使用“引用”要注意哪些问题? 答:引用就 ...

  5. 时间戳转为C#格式时间

    经常发现很多地方使用一个时间戳表示时间.比如: 1370838759 表示 2013年6月10日 12:32:39. 我们就需要一个工具,方便地转换这种时间格式 什么是时间戳? 时间戳, 又叫Unix ...

  6. ZooKeeper实现分布式队列Queue

    ZooKeeper实现分布式队列Queue 让Hadoop跑在云端系列文章,介绍了如何整合虚拟化和Hadoop,让Hadoop集群跑在VPS虚拟主机上,通过云向用户提供存储和计算的服务. 现在硬件越来 ...

  7. PostgreSQL的HOT(Heap-Only Tuples)

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL内部结构与源代码研究索引页    回到顶级页面:PostgreSQL索引页 HOT的解释: 如下的日文文档中,有几个图示, ...

  8. 实现对象属性的lazy-loading(延迟加载)

    一.延迟加载器LazyLoader作用:       说到延迟加载,应该经常接触到,尤其是使用Hibernate的时候,本篇将通过一个实例分析延迟加载的实现方式.LazyLoader接口继承了Call ...

  9. JAVA基础 XML生成与解析和String包装类下 .replace方法的使用以及char和字符序列的使用场景

    ptLink0.setText(arbu.getPtLink().replace("&","&")); // 如果像 '&','& ...

  10. 对RedisTemplate接口二次封装成自定义工具接口

    开发过程中,经常使用redis数据库存储. 一般都是依赖注入redisTemplate,然后调用redisTemplate的api进行接口功能实现. 但是,如果对redisTemplate自带的API ...