【搜索】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 ...
随机推荐
- 开发自己的DataSet查看器
记得在vs2002不是2003上没有DataSet调试器,断点时查看DataSet内容非常麻烦,最后有人开发了第三方工具解决了此问题. 后续的vs版本内部都自带的此工具可查看DataSet/DataT ...
- myeclipse(eclipse)IDE配置
1.更换JDK8 2.更换低版本的eclipse试试 其他方法暂时没想到 一.设置编码与字体 工作空间编码设置:window->perferences->General->words ...
- 【HNOI2011】卡农
题面 题解 将无序化为有序,最后答案除以$m!$. 设$f[i]$表示选出了$i$个子集,并且满足所有的限制的方案数. 因为转移困难,所以考虑容斥 限制了每个数的出现次数为偶数,所以如果前$i - 1 ...
- 【BZOJ1050】[HAOI2006]旅行
[BZOJ1050][HAOI2006]旅行 题面 bzoj 洛谷 题解 先将所有边从小往大排序 枚举钦定一条最小边 再枚举依次枚举最大边,如果两个点联通了就\(break\)统计答案即可 代码 #i ...
- HDU - 5877 Weak Pair (dfs+树状数组)
题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...
- Linux 挂载 xshell 命令 配置环境变量
- 巧用 Python 找工作(资料在文末)
前言 近年来 Python 之火大家都有感而知,那亲们知道北京的 Python 开发岗位.运维开发岗位招聘地域都是如何分布的吗?薪水如何?是否有前景等等,这些数据呢直接通过招聘信息来了解到企业用人是最 ...
- c#_导出table功能
一:第一张导出方法,简单快捷 请注意:一般表格都有真分页,查询数据时候注意把分页条件去掉#region 此处是获取的list数组 然后转table再调用ExportExcel var list=&qu ...
- Map.containsKey(String key)
判断key有没有对应的value值有,返回true无,返回false
- python游戏编程——乌龟和鱼类场景编程
综合举例: 游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏. O 假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10 · 游戏生成1只 ...