一、题目

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

  1. 4
  2. 0 0 2
  3. 2 1 2
  4. 1 1 2
  5. 0 3 5

Sample Output

  1. 5

二、思路

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

二、思路&心得

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

三、代码


  1. int dist[MAX_SIZE][MAX_SIZE];
  2. int dirction[5][2] = {0, -1, -1, 0, 0, 1, 1, 0, 0, 0};
  3. typedef pair<int, int> P;
  4. struct Meteor {
  5. int X;
  6. int Y;
  7. int T;
  8. } meteors[MAX_M];
  9. bool cmp (Meteor a, Meteor b) {
  10. if (a.T < b.T) return true;
  11. else return false;
  12. }
  13. bool isLegal(int x, int y) {
  14. if (x >= 0 && x < MAX_SIZE && y >= 0 && y < MAX_SIZE) return true;
  15. else return false;
  16. }
  17. int bfs() {
  18. queue<P> que;
  19. if (map[0][0] == 0) return -1;
  20. else if (map[0][0] == MAX_TIME) return 0;
  21. que.push(P(0, 0));
  22. while (que.size()) {
  23. P p = que.front();
  24. que.pop();
  25. int px = p.first, py = p.second;
  26. if (px >= MAX_SIZE || py >= MAX_SIZE) continue;
  27. for(int i = 0; i < 4; i ++) {
  28. int tx = px + dirction[i][0], ty = py + dirction[i][1];
  29. if (isLegal(tx, ty)) {
  30. if (map[tx][ty] == MAX_TIME) {
  31. return ++ dist[px][py];
  32. }
  33. if (dist[px][py] + 1 < map[tx][ty] && !dist[tx][ty]) {
  34. que.push(P(tx, ty));
  35. dist[tx][ty] = dist[px][py] + 1;
  36. }
  37. }
  38. }
  39. }
  40. return -1;
  41. }
  42. void solve() {
  43. for (int i = 0; i < MAX_SIZE; i ++) {
  44. for (int j = 0; j < MAX_SIZE; j ++) {
  45. map[i][j] = MAX_TIME;
  46. }
  47. }
  48. for (int i = 0; i < M; i ++) {
  49. scanf("%d %d %d", &meteors[i].X, &meteors[i].Y, &meteors[i].T);
  50. }
  51. sort(meteors, meteors + M, cmp);
  52. for (int i = 0; i < M; i ++) {
  53. for (int j = 0; j < 5; j ++) {
  54. int tx = meteors[i].X, ty = meteors[i].Y;
  55. tx += dirction[j][0];
  56. ty += dirction[j][1];
  57. if (isLegal(tx, ty) && map[tx][ty] > meteors[i].T) {
  58. map[tx][ty] = meteors[i].T;
  59. }
  60. }
  61. }
  62. printf("%d\n", bfs());
  63. }
  64. int main() {
  65. while (~scanf("%d", &M)) {
  66. solve();
  67. }
  68. return 0;
  69. }

【搜索】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. 大数据调错系列之:自己总结的myeclipse连接hadoop会出现的问题

    在我们学习或者工作中开始hadoop程序的时候,往往会遇到一个问题,我们写好的程序需要打成包放在集群中运行,这无形中在浪费我们的时间,因为程序可以需要不断的调试,然后把最终程序放在集群中即可.为了解决 ...

  2. Spring第四天——SSH整合

    (从整合开始,使用回归使用eclipse) 一.三大框架版本:  struts2 hibernate5 spring4 二.SSH三大框架回顾: Hibernate: ORM思想 核心配置文件: 单独 ...

  3. Matlab zeros ones

    zeros函数——生成零矩阵 ones函数——生成全1阵 [zeros的使用方法] B=zeros(n):生成n×n全零阵. B=zeros(m,n):生成m×n全零阵. B=zeros([m n]) ...

  4. RabbitMQ(一):Window安装RabbitMQ

    原文:RabbitMQ(一):Window安装RabbitMQ 1.安装ERLANG语言环境 由于RabbitMQ是采用Erlang编写的,因此我们需要先安装该语言库,以便运行代理服务器.从Erlan ...

  5. 苏州Uber优步司机奖励政策(4月22日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 基于bootstrap的文本编辑器组件:Summernote

    Summernote官网地址 :https://summernote.org/ 这是官网的一个例子: <!DOCTYPE html> <html lang="en" ...

  7. 14 [网络编程]-socket

    1.互联网协议 博客:http://www.cnblogs.com/linhaifeng/articles/6129246.html 2.socket层 Socket是应用层与TCP/IP协议族通信的 ...

  8. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

  9. UWP 检测网络状态

    最近发现Community Toolkit有了网络辅助类,貌似很早就有了... 很不错,还是用.给大家分享一下. 1. 检测网络是否可用 2. 检测网络是否是计费模式? 3. 检测网络接入类型 4. ...

  10. mtr语言真是逆天了

    实践证明,设计一个语言,还不是简单的解释没一行哦