POJ 3669

题意:已知流星的颗数,落地的坐标和落地时间,若流星落在某点则该点周围的四点也会被波及,现在要求你到达一点没有被流星击过的点最少时间

注意:只能在第一象限内包括X,Y轴的正半轴

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cctype>
 5 #include <cmath>
 6 #include <time.h>
 7 #include <string>
 8 #include <map>
 9 #include <stack>
10 #include <set>
11 #include <queue>
12 #include <vector>
13 #include <algorithm>
14 #include <iostream>
15 using namespace std;
16 typedef long long ll;
17 typedef pair<int,int> P;
18 #define PI acos( -1.0 )
19 const double E = 1e-8;
20 const int INF = 0x7fffffff;
21
22 const int NO = 300 + 5;
23 int m[NO][NO];
24 int step[NO][NO];
25 int mark[NO][NO];
26 int dir[][2] = { {-1,0}, {1,0}, {0,-1}, {0,1} };
27 int n;
28
29 void init()
30 {
31     for( int i = 0; i < NO; ++i )
32         for( int j = 0; j < NO; ++j )
33             m[i][j] = INF;
34 }
35
36 void init1( int x, int y, int c )
37 {
38     for( int i = 0; i < 4; ++i )
39     {
40         int xx = x + dir[i][0];
41         int yy = y + dir[i][1];
42         if( xx >= 0 && yy >= 0 )
43             m[xx][yy] = min( m[xx][yy], c );
44     }
45 }
46
47 void bfs()
48 {
49     queue <P> q;
50     q.push( P( 0, 0 ) );
51     mark[0][0] = 0;
52     memset( step, 0, sizeof( step ) );
53     while( !q.empty() )
54     {
55         P t = q.front(); q.pop();
56         int x = t.first;
57         int y = t.second;
58         mark[x][y] = 0;
59         if( m[x][y] == INF )
60         {
61             printf( "%d\n", step[x][y] );
62             return;
63         }
64         for( int i = 0; i < 4; ++i )
65         {
66             int dx = x + dir[i][0];
67             int dy = y + dir[i][1];
68             if( dx >= 0 && dy >= 0 && ( m[dx][dy] == INF || m[dx][dy] > step[x][y]+1 ) && !mark[dx][dy] )
69             {
70                 mark[dx][dy] = 1;
71                 step[dx][dy] = step[x][y] + 1;
72                 q.push( P( dx, dy ) );
73             }
74         }
75     }
76     puts( "-1" );
77 }
78
79 int main()
80 {
81     scanf( "%d", &n );
82     int a, b, c;
83     init();
84     for( int i = 0; i < n; ++i )
85     {
86         scanf( "%d%d%d", &a, &b, &c );
87         m[a][b] = min( m[a][b], c );
88         init1( a, b, c );
89     }
90     bfs();
91     return 0;
92 }

时间: 2024-10-14 00:19:02

POJ 3669的相关文章

POJ 3669 Meteor Shower【BFS】

POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最早被炸毁的时间 #include <iostream> #include <algorithm> #include <queue> #include <cstring> using namespace std; #define INDEX_MAX 512 int

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

POJ 3669 Meteor Shower 题解 《挑战程序设计竞赛》

题目链接: http://poj.org/problem?id=3669 题意: 这个同学发现流星雨要来了,会对地球造成伤害于是决定逃跑.N颗流星会不定时降落在坐标轴第一象限300*300内的点上.给出每颗流星降落的坐标和时刻,求这个同学能否成功逃跑,能的话用时多少. 思路: 略有一点tricky,无法变通了(@﹏@)~.看了码农场的代码. 这道题没有给出地图,所以需要自己生成地图. 关键的点在于:在生成地图的时候,将每个点的初始值设为无穷大(表示不会有流星),将要被摧毁的点设置为流星降落的时刻

【POJ 3669 Meteor Shower】简单BFS

流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封锁).安全地带为永远不会被封锁的点. 简单bfs,开始WA在把平面空间上限当成300*300,但根据题目,这只是有流星雨撞击的范围.实际可走的空间理论上没上限,但分析可得,离原点最近的安全地带一定在(302,302)范围内,所以应可把数组至少开为303*303. 后来WA在把G[0][0]==1的情

POJ - 3669 Meteor Shower(bfs)

题意:某人在时刻0从原点出发,在第一象限范围内移动.已知每个炸弹爆炸的地点和时刻,炸弹爆炸可毁坏该点和它上下左右的点.不能经过已毁坏的点,也不能在某点毁坏的时候位于该点.不会被炸弹毁坏的地方是安全之处,问某人到达安全之处的最短时间. 分析:bfs即可.注意:1.某点多次爆炸或受爆炸点影响而毁坏,取最早时间为该点的毁坏时间.2.bfs走过的点不能再走,注意标记. #pragma comment(linker, "/STACK:102400000, 102400000") #include

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

poj 3669 Meteor Shower

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 h

POJ 3669 Meteor shower 简单BFS, 有坑点

Meteor Shower 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

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