Meteor Shower(POJ 3669)

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

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

Sample Output

5题意思路:m个陨石将砸向第一象限和正坐标抽,每块陨石砸的时间分别为Ti,从0,0出发,如何走才能达到安全位置;用一个T[][]数组记录每个点的最早爆炸时间,vis标记该点是否被砸,如果该点不可能被砸,那么为安全位置,否则向四个方向扩展,改点可走的条件为改点没有走过以及该点爆炸的时间大于当前时间,对于走过的点标记T[][]为0,那么改点在以后就不可能再走。

反正我的思路好麻烦,每次都要写个函数对所有陨石进行遍历判断是否为安全位置。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 >= 0 && y >= 0;
37 }
38 bool vis[301][301];
39 int Move[4][2] = {0,1, 0,-1, 1,0, -1,0};
40 int T[301][301];
41 int BFS(int x, int y)
42 {
43     if(!vis[x][y])
44         return 0;
45     queue<Node> Q;
46     Node now, next;
47     now.x = x; now.y = y; now.step = 0;
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 = 0; k < 4; k++)
56         {
57             next.x = now.x + Move[k][0];
58             next.y = now.y + Move[k][1];
59             next.step = now.step + 1;
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] = 0;
66                     Q.push(next);
67                 }
68             }
69             else
70                 return next.step;
71         }
72     }
73     return -1;
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 = 0; 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 = 0; k < 4; k++)
88             {
89                 int xx = x + Move[k][0];
90                 int yy = y + Move[k][1];
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(0, 0));
97     }
98     return 0;
99 }

WA:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <queue>
  6 using namespace std;
  7 int vis[305][305],b[305][305];
  8 struct node {int x,y,t;}m[50005];
  9 bool cmp(node a,node b)    {return a.t<=b.t;}
 10 int n,Mint;
 11 int dx[]={0,0,0,1,-1},dy[]={0,1,-1,0,0};
 12 bool safe(node p)
 13 {
 14     int l=-1,r=n,mid;
 15     while(r-l>1)
 16     {
 17         mid=(l+r)/2;
 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=0;j<5;j++)
 26         {
 27             if(p.x==(m[i].x+dx[j])&&p.y==(m[i].y+dy[j]))
 28                 return 0;
 29         }
 30     }
 31     return 1;
 32 }
 33 int bfs()
 34 {
 35     int i,j;
 36     Mint=-1;
 37     queue<node> s;
 38     node temp,next;
 39     temp.x=temp.y=temp.t=0;
 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=-1,r=n,mid;
 52         while(r-l>1)
 53         {
 54             mid=(l+r)/2;
 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=0;i<=4;i++)
 64                 b[m[r].x+dx[i]][m[r].y+dy[i]]=1;
 65             r++;
 66         }
 67         for(i=1;i<=4;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>=0&&next.y>=0&&vis[next.x][next.y]==0&&b[next.x][next.y]==0)
 73             {
 74                 vis[next.x][next.y]=1;
 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,0,sizeof(vis));
 89         memset(b,0,sizeof(vis));
 90         for(i=0;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=0;
 96         bool flag=0;
 97         while(m[i].t==0)
 98         {
 99             if((m[i].x==0&&m[i].y==0)||(m[i].x==0&&m[i].y==1)||(m[i].x==1&&m[i].y==0))
100             {
101                 cout<<-1<<endl;
102                 flag=1;
103                 break;
104             }
105             i++;
106         }
107         if(flag)
108             continue;
109         bfs();
110         printf("%d\n",Mint);
111     }
112 }
时间: 2024-10-10 13:44:19

Meteor Shower(POJ 3669)的相关文章

POJ 3669 Meteor Shower (BFS + 预处理)

Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9677   Accepted: 2718 Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hi

POJ 3669 Meteor Shower(流星雨)

POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K 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 sa

HDU 1325 Is It A Tree? (POJ 1308)

并查集问题... 这题以前做过-- 以前做过-- 做过-- 过-- 不过重做时候被吭得异常之爽-- 在判断 vis[i]的时候.我记得标准C++是非0 即为真. 而我用C++ 提交的时候 if(vis[i]) 去直接给我WA了. 用G++ 就AC了...然后改成if(vis[i]==1) 交C++ 就AC了. 特瞄的我每次初始化都把 vis[i] 都赋值为 0 了..都能出这种错? 求路过大神明示我的错误. 题意是判断是否是一棵树. 不能存在森林,用并查集合并,每个点的入度不能超过1. 比如 1

HDU 1535 Invitation Cards (POJ 1511)

两次SPFA.求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换,但是这个有1000000个点,矩阵开不了. d1[]为 1~N 的最短路. 将所有边的 邻点 交换. d2[] 为 1~N 的最短路. 所有相加为 所要答案. 忧伤的是用SPFA  "HDU 1535"  AC了,但是POJ 一样的题 "POJ 1511" 就WA了. 然后强迫症犯了,不停的去测试. 题意中找到一句关键话 :Prices are positive integ

每日一dp(1)——Largest Rectangle in a Histogram(poj 2559)使用单调队列优化

Largest Rectangle in a Histogram 题目大意: 有数个宽为1,长不定的连续方格,求构成的矩形中最大面积 /************************************************************************/ /* 思路1. 当前为n的面积如何与n-1相联系,dp[i][j]=max(dp[i-1][k]) , 0<k<=j 描述:i为方块个数,j为高度 但是此题目的数据对于高度太变态,h,1000000000 ,n,1

Power string(poj 2406)

题目大意,给出一个字符串s,求最大的k,使得s能表示成a^k的形式,如 abab 可以表示成(ab)^2: 方法:首先 先求kmp算法求出next数组:如果 len mod (len-next[len])==0 ,答案就是 len /(len-next[len]),否则答案是1:证明如下: 如果s能表示成 a^k的形式且k>1,k尽可能大,即s可以表示成aaaaaa(k个a):那么next[len]就等于k-1个a的长度:aaaaaaa  aaaaaaa那么 (len-next[len])a的长

(多重背包+记录路径)Charlie&#39;s Change (poj 1787)

http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your

Feel Good (poj 2796)

题目大意就是在给出的串中找出一段连续数字,使得 这一段的和 乘上 这一段最小的数 的结果最大. 可以用rmq做.每次区间找当中最小的数,算出值并记录位置.然后再递推它的左右区间. 不过- -,一开始用深搜递推RE了.栈空间不够了,然后慢慢优化,最后还是ac了. 貌似这一题是用单调栈做的,还可以用查并集做...我也是醉了 具体看代码吧 1 /************************************************************************* 2 > F

poj 3669 Meteor Shower(bfs)

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 destroy