【搜索】POJ-3669 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 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 (Xi, Yi) (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: Xi, Yi, 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的更多相关文章
- poj 3669 bfs(这道题隐藏着一个大坑)
题意 在x,y坐标系,有流星会落下来,给出每颗流星落下来的坐标和时间,问你能否从(0,0)这个点到一个安全的位置.所谓的安全位置就是不会有流星落下的位置. 题解: 广搜,但是这里有一个深坑,就是搜索的 ...
- POJ 3669 Meteor Shower【BFS】
POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...
- POJ 3669 广度优先搜索
题意:巨大流星雨即将袭来.每个流星会对击中的地方以及周围(上下左右四格)造成破坏.Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方(在第一象限内).已知每移动一格需要1个时间单 ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- POJ 1475 Pushing Boxes 搜索- 两重BFS
题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...
- poj 3249(bfs+dp或者记忆化搜索)
题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...
- POJ 3669 Meteor Shower BFS 水~
http://poj.org/problem?id=3669 题目大意: 一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达 ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
随机推荐
- 记Macbook Pro配合FT232使用PN532模块
0x00实话. 被自己傻到 在linux下使用 libusb + libnfc 这两个库就可以配合串口直接使用pn532模块 当时配置文件是写在 /etc/nfc 目录下的 而我在OS X 下居然也想 ...
- php使用urlencode对中文编码而引出的问题:urlencode和rawurlencode的区别
事件背景: 之前做h5小游戏,需要后端输出用户的相关信息给前端,输出的内容有:用户id,用户昵称等字段,使用get方式传参.后端使用PHP语言对中文昵称进行格式化编码,使用的是常用的urlencode ...
- linux-2.6内核驱动学习——jz2440之按键
//以下是学习完韦东山老师视屏教程后所做学习记录中断方式取得按键值: #include <linux/module.h> #include <linux/kernel.h> ...
- 搜集到的一些python资料
1,MOOC课程-Python语言程序设计(嵩天)http://www.icourse163.org/course/BIT-268001 2,Python123网站(嵩天老师的教学网站):https: ...
- kotlin使用anko在Android中实现Activity跳转,超优雅!
//正常跳转 startActivity<RegisterActivity>() //携带参数 startActivity<ResetPwdActivity>("ke ...
- Scala中的类学习
Scala中的类学习 从java了解类的情况下,了解Scala的类并不难.Scala类中的字段自动带getter和setter方法,用@BeanProperty注解生成javaBean对象的getXX ...
- [agc011E]Increasing Numbers-[思考题]
Description 传送门 Solution 依题得所有不下降数(设为a)可以拆为若干个全1数的和(如:1558=1111+111+111+111+111+1+1+1) 并且任意a所能拆出的全一数 ...
- 洛谷 P3795 钟氏映射
洛谷 P3795 钟氏映射 题目背景 2233年,CSSYZ学校的数学老师兼数学竞赛顾问钟JG已经2200+岁啦! 为了庆生,他或她给广大人民群众出了道题. 题目描述 设集合N=M={x∣x∈N+, ...
- Velocity学习2
Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象. 当Veloci ...
- 洛咕 P2467 [SDOI2010]地精部落
同波浪,简单dp. 高度从1到n插入山脉,设f[i][j][k]表示插入了i个山脉,组成了j段,边界上有k个山脉的方案数. 那么新插入的山脉只会:插入在边界上且自己是一段.插入在边界上且与最左边的段相 ...