一、题目

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 ≤ *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: 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

二、思路

  • 安全区可以到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的更多相关文章

  1. poj 3669 bfs(这道题隐藏着一个大坑)

    题意 在x,y坐标系,有流星会落下来,给出每颗流星落下来的坐标和时间,问你能否从(0,0)这个点到一个安全的位置.所谓的安全位置就是不会有流星落下的位置. 题解: 广搜,但是这里有一个深坑,就是搜索的 ...

  2. POJ 3669 Meteor Shower【BFS】

    POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...

  3. POJ 3669 广度优先搜索

    题意:巨大流星雨即将袭来.每个流星会对击中的地方以及周围(上下左右四格)造成破坏.Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方(在第一象限内).已知每移动一格需要1个时间单 ...

  4. POJ 3669 Meteor Shower(流星雨)

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

  5. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  6. POJ 1475 Pushing Boxes 搜索- 两重BFS

    题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...

  7. poj 3249(bfs+dp或者记忆化搜索)

    题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...

  8. POJ 3669 Meteor Shower BFS 水~

    http://poj.org/problem?id=3669 题目大意: 一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达 ...

  9. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

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

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

随机推荐

  1. ~ManancherQwQ

    例题传送门 好久没写博客了,写一发. 之前学的Manacher算法拿到模板题找到那最后一个点的错误了,TMD没开long long. Manachar算法主要是处理字符串中关于回文串的问题的,它可以在 ...

  2. Catalan&Stirling数

    Catalan&Stirling数 Tags:数学 作业部落 评论地址 Catalan数 \(1,1,2,5,14,42,132,429,1430,4862,16796,58786...\) ...

  3. 成都Uber优步司机奖励政策(4月21日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. 26-[Boostrap]-介绍与起步

    1.Bootstrap的介绍 凡是使用过Bootstrap的开发者,都不在乎做这么两件事情:复制and粘贴.哈哈~,是的使用Bootstrap非常简单,但是在复制粘贴之前,需要先对Bootstrap的 ...

  5. 2597: [Wc2007]剪刀石头布

    2597: [Wc2007]剪刀石头布 链接 分析: 费用流. 首先转化一下问题,整张图最优的情况是存在$C_n^3$个,即任意3个都行,然后考虑去掉最少不满足的三元环. 如果u赢了v,u向v连一条边 ...

  6. spring在service层获取session和request

    首先要在web.xml增加如下代码: <listener> <listener-class>org.springframework.web.context.request.Re ...

  7. SQL Server 的通用分页显示存储过程(转载)

    http://database.51cto.com/art/200512/12923.htm 建立一个 Web 应用,分页浏览功能必不可少.这个问题是数据库处理中十分常见的问题.经典的数据分页方法是: ...

  8. 百度ueditor 文本框

    所需配置(qui框架) <!--ueEditor编辑器start--> <script>  window.UEDITOR_HOME_URL = ctx+"/stati ...

  9. 洛谷P1313 计算系数【快速幂+dp】

    P1313 计算系数 题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别 ...

  10. idou老师教你学Istio:如何用 Istio 实现速率限制

    使用 Istio 可以很方便地实现速率限制.本文介绍了速率限制的使用场景,使用 memquota\redisquota adapter 实现速率限制的方法,通过配置 rule 实现有条件的速率限制,以 ...