Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12816   Accepted: 3451

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 ≤ X≤ 300; 0 ≤ Y≤ 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
    题意思路:m个陨石将砸向第一象限和正坐标抽,每块陨石砸的时间分别为Ti,从00出发,如何走才能达到安全位置;
    用一个T[][]数组记录每个点的最早爆炸时间,vis标记该点是否被砸,如果该点不可能被砸,那么为安全位置,否则向四个方向扩展,改点可走的条件为改点没有走过以及该点爆炸的时间大于当前时间,对于走过的点标记T[][]为0,那么改点在以后就不可能再走。
  2.  
  3. 反正我的思路好麻烦,每次都要写个函数对所有陨石进行遍历判断是否为安全位置。
    AC:
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <algorithm>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #define INF 0x3f3f3f
  12. #define eps 1e-8
  13. #define MAXN (50000+1)
  14. #define MAXM (100000)
  15. #define Ri(a) scanf("%d", &a)
  16. #define Rl(a) scanf("%lld", &a)
  17. #define Rf(a) scanf("%lf", &a)
  18. #define Rs(a) scanf("%s", a)
  19. #define Pi(a) printf("%d\n", (a))
  20. #define Pf(a) printf("%.2lf\n", (a))
  21. #define Pl(a) printf("%lld\n", (a))
  22. #define Ps(a) printf("%s\n", (a))
  23. #define W(a) while(a--)
  24. #define CLR(a, b) memset(a, (b), sizeof(a))
  25. #define MOD 1000000007
  26. #define LL long long
  27. #define lson o<<1, l, mid
  28. #define rson o<<1|1, mid+1, r
  29. #define ll o<<1
  30. #define rr o<<1|1
  31. using namespace std;
  32. struct Node{
  33. int x, y, step;
  34. };
  35. bool judge(int x, int y){
  36. return x >= && y >= ;
  37. }
  38. bool vis[][];
  39. int Move[][] = {,, ,-, ,, -,};
  40. int T[][];
  41. int BFS(int x, int y)
  42. {
  43. if(!vis[x][y])
  44. return ;
  45. queue<Node> Q;
  46. Node now, next;
  47. now.x = x; now.y = y; now.step = ;
  48. Q.push(now);
  49. while(!Q.empty())
  50. {
  51. now = Q.front();
  52. Q.pop();
  53. if(!vis[now.x][now.y])
  54. return now.step;
  55. for(int k = ; k < ; k++)
  56. {
  57. next.x = now.x + Move[k][];
  58. next.y = now.y + Move[k][];
  59. next.step = now.step + ;
  60. if(!judge(next.x, next.y)) continue;
  61. if(vis[next.x][next.y])
  62. {
  63. if(T[next.x][next.y] != INF && next.step < T[next.x][next.y])
  64. {
  65. T[next.x][next.y] = ;
  66. Q.push(next);
  67. }
  68. }
  69. else
  70. return next.step;
  71. }
  72. }
  73. return -;
  74. }
  75. int main()
  76. {
  77. int n;
  78. while(Ri(n) != EOF)
  79. {
  80. CLR(vis, false); CLR(T, INF);
  81. for(int i = ; i < n; i++)
  82. {
  83. int x, y, t;
  84. Ri(x); Ri(y); Ri(t);
  85. T[x][y] = min(T[x][y], t);
  86. vis[x][y] = true;
  87. for(int k = ; k < ; k++)
  88. {
  89. int xx = x + Move[k][];
  90. int yy = y + Move[k][];
  91. if(!judge(xx, yy)) continue;
  92. vis[xx][yy] = true;
  93. T[xx][yy] = min(T[xx][yy], t);
  94. }
  95. }
  96. Pi(BFS(, ));
  97. }
  98. return ;
  99. }

WA:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <queue>
  6. using namespace std;
  7. int vis[][],b[][];
  8. struct node {int x,y,t;}m[];
  9. bool cmp(node a,node b) {return a.t<=b.t;}
  10. int n,Mint;
  11. int dx[]={,,,,-},dy[]={,,-,,};
  12. bool safe(node p)
  13. {
  14. int l=-,r=n,mid;
  15. while(r-l>)
  16. {
  17. mid=(l+r)/;
  18. if(m[mid].t>=p.t)
  19. r=mid;
  20. else
  21. l=mid;
  22. }
  23. for(int i=r;i<n;i++)
  24. {
  25. for(int j=;j<;j++)
  26. {
  27. if(p.x==(m[i].x+dx[j])&&p.y==(m[i].y+dy[j]))
  28. return ;
  29. }
  30. }
  31. return ;
  32. }
  33. int bfs()
  34. {
  35. int i,j;
  36. Mint=-;
  37. queue<node> s;
  38. node temp,next;
  39. temp.x=temp.y=temp.t=;
  40. s.push(temp);
  41. while(!s.empty())
  42. {
  43. temp=s.front();
  44. s.pop();
  45. if(safe(temp))
  46. {
  47. Mint=temp.t;
  48. break;
  49. }
  50. temp.t++;
  51. int l=-,r=n,mid;
  52. while(r-l>)
  53. {
  54. mid=(l+r)/;
  55. if(m[mid].t>=temp.t)
  56. r=mid;
  57. else
  58. l=mid;
  59. }
  60. while(m[r].t==temp.t)
  61. {
  62.  
  63. for(int i=;i<=;i++)
  64. b[m[r].x+dx[i]][m[r].y+dy[i]]=;
  65. r++;
  66. }
  67. for(i=;i<=;i++)
  68. {
  69. next.x=temp.x+dx[i];
  70. next.y=temp.y+dy[i];
  71. next.t=temp.t;
  72. if(next.x>=&&next.y>=&&vis[next.x][next.y]==&&b[next.x][next.y]==)
  73. {
  74. vis[next.x][next.y]=;
  75. s.push(next);
  76. }
  77. //cout<<"ads";
  78. }
  79. }
  80. return Mint;
  81. }
  82. int main()
  83. {
  84. int i,j;
  85. freopen("in.txt","r",stdin);
  86. while(scanf("%d",&n)!=EOF)
  87. {
  88. memset(vis,,sizeof(vis));
  89. memset(b,,sizeof(vis));
  90. for(i=;i<n;i++)
  91. scanf("%d%d%d",&m[i].x,&m[i].y,&m[i].t);
  92. sort(m,m+n,cmp);
  93. //for(i=0;i<n;i++)
  94. // cout<<m[i].x<<" "<<m[i y<<" "<<m[i].t<<endl;
  95. i=;
  96. bool flag=;
  97. while(m[i].t==)
  98. {
  99. if((m[i].x==&&m[i].y==)||(m[i].x==&&m[i].y==)||(m[i].x==&&m[i].y==))
  100. {
  101. cout<<-<<endl;
  102. flag=;
  103. break;
  104. }
  105. i++;
  106. }
  107. if(flag)
  108. continue;
  109. bfs();
  110. printf("%d\n",Mint);
  111. }
  112. }

Meteor Shower(POJ 3669)的更多相关文章

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

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

  2. poj3669 Meteor Shower(预处理+bfs)

    https://vjudge.net/problem/POJ-3669 先给地图a[][]预处理每个位置被砸的最小时间.然后再bfs. 纯bfs,还被cin卡了下时间.. #include<io ...

  3. POJ 3669 Meteor Shower(流星雨)

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

  4. poj 3669 Meteor Shower(bfs)

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

  5. 【POJ - 3669】Meteor Shower(bfs)

    -->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...

  6. 题解报告:poj 3669 Meteor Shower(bfs)

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

  7. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  8. Scout YYF I(POJ 3744)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1553 Descr ...

  9. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

随机推荐

  1. UVa 232 Crossword Answers

     Crossword Answers  A crossword puzzle consists of a rectangular grid of black and white squares and ...

  2. 观《Terminal》之感

    读书笔记系列链接地址http://www.cnblogs.com/shoufengwei/p/5714661.html.        经人推荐,用了几天时间欣赏了这部斯皮尔伯格导演的电影<Te ...

  3. MySql增加字段、删除字段、修改字段

    MySql增加字段.删除字段.修改字段名称.修改字段类型   1.增加一个字段 alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; / ...

  4. yii开启gii功能

    如果不想面对黑白界面,那么yii框架,给我们提供了一个模块gii 在配置文件中main.php 再通过访问模块的方式访问gii

  5. poj3294 --Life Forms

    Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12483   Accepted: 3501 Descr ...

  6. shell操作mysql

    参考: http://blog.csdn.net/hbcui1984/article/details/5125387

  7. pyqt QTableView例子学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import  * from Py ...

  8. IOS 采用https 协议访问接口

    申请好证书后,发现ios 仍无法使用https协议访问到数据,发现ios 需要ssl 支持 TLS1.2 . 更改nginx 配置: ssl_protocols TLSv1 TLSv1. TLSv1. ...

  9. JS操作URL

    function getQueStr(url, ref) //取获参数值 { ); ) { var arr = str.split('&'); for (i in arr) { ] == re ...

  10. 转化json

    /// <summary> /// 转换成JSON字符串 /// </summary> /// <param name="jsonObject"> ...