#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std; const int maxn = ;
const int INF = ;
int M;
struct Meteor {
int X, Y;
int Time; //Time_i
Meteor(int X = , int Y = , int T = ) :
X(X), Y(Y), Time(T) {}
bool operator < (const Meteor& b) { // 升序排序
return Time < b.Time;
}
} meteor[ + ];
int dir[][] = {{-, }, {, }, {, }, {, -}, {, }};
bool used[maxn][maxn];
int field[maxn][maxn]; //标记好每个位置被流星砸(以及扩散到)的时间
int last;
int ans; void input();
void solve();
bool check(int r, int c, int T);
int BFS(int r, int c, int T); void input()
{
memset(used, false, sizeof(used));
for (int i = ; i < maxn; i++) {
for (int j = ; j < maxn; j++) {
field[i][j] = INF;
}
}
scanf("%d", &M);
for (int i = ; i < M; i++) {
scanf("%d%d%d", &meteor[i].X, &meteor[i].Y, &meteor[i].Time);
}
sort(meteor, meteor + M); //按照时间升序排序
last = meteor[M - ].Time; //最后被毁灭的时间 for (int i = ; i < M; i++) //M个火球
{
for (int j = ; j < ; j++) { //5个方向
int x = meteor[i].X + dir[j][],
y = meteor[i].Y + dir[j][];
if ( check(x, y, meteor[i].Time) ) {
field[x][y] = meteor[i].Time; //x,y位置和周围扩散位置,破坏的时间
}
}
}
} bool check(int r, int c, int T)
{
return r >= && c >= && field[r][c] > T;
} int BFS(int r, int c, int T)
{
used[r][c] = true; //从原点开始
queue<Meteor> que;
Meteor cur;
cur.X = r, cur.Y = c, cur.Time = T;
que.push(cur); //开始的位置,和时间 while (!que.empty())
{
Meteor m = que.front(); que.pop();
for (int i = ; i < ; i++) { //遍历4个方向, 因为最后一个方向,是在原地
cur = m;
cur.X = m.X + dir[i][],
cur.Y = m.Y + dir[i][]; //周围位置
cur.Time++; //移到下个位置的时间
//如果火球落到该位置的时间>当前人的时间,表示还有机会移动
if (check(cur.X, cur.Y, cur.Time) && !used[cur.X][cur.Y]) {
used[cur.X][cur.Y] = true;
//说明当前位置时间永远不会被炸到
//last是最后被炸到的时间
if (field[cur.X][cur.Y] > last) {
return cur.Time; //则返回当前到达安全的时间
}
que.push(cur); //否则入队列
}
}
}
return -; //不存在安全位置
} void solve()
{
input();
if (field[][] == ) { //原点就被毁灭, 反应时间为0
printf("-1\n");
}
else {
printf("%d\n", BFS(, , ));
}
} int main()
{
solve();
return ;
}

分析:1. 还是经典的BFS问题,主要是要 对被摧毁的位置的时间进行记录(先升序处理)(以及波及到的位置进行时间标志).

   2. 人行打算走下一步的时候, 先判断是否时间允许,允许标志为访问过(允许的时候,需要当前位置时间是否已经超过了 最后被毁灭位置的时间,是则返回 到达该安全位置的时间)。不允许则添加到队列中。

题目链接: http://poj.org/problem?id=3669

参考了这篇博客: http://www.cnblogs.com/ZefengYao/p/5935161.html

BFS搜索:POJ No 3669 Meteor Shower的更多相关文章

  1. POJ 3669 Meteor Shower(流星雨)

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

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

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

  3. 题解报告:poj 3669 Meteor Shower(bfs)

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

  4. POJ 3669 Meteor Shower BFS求最小时间

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31358   Accepted: 8064 De ...

  5. poj 3669 Meteor Shower(bfs)

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

  6. poj 3669 Meteor Shower

                                                                                                      Me ...

  7. POJ 3669 Meteor Shower【BFS】

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

  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)不能再经过(被封 ...

随机推荐

  1. thinkphp学习2-控制器

    1.定义控制器 一般来说,ThinkPHP的控制器是一个类,而操作则是控制器类的一个公共方法. 控制器通常需要继承系统的Controller类或者其子类,例如,下面定义了一个 \Home\Contro ...

  2. iOS- 如何使用Alcatraz来高效的管理Xcode-Plugin(Xcode插件)

    1.前言 相信各位iOS攻城师用的Xocde的快捷插件也不少,今天向大家分享一款能高效快捷的管理Xcode-Plugin的软件<Alcatraz>,自己亲自体验后,爱不释手.   (这里用 ...

  3. scrapy 直接在编辑器运行

    # *_*coding:utf-8 *_* from scrapy.cmdline import execute execute("scrapy crawl spbeen --nolog&q ...

  4. IPV6 国内进展情况

    国家下一代互联网产业技术创新战略联盟(以下简称“产业联盟”),近日在北京发布了我国首份IPv6业务用户体验监测报告(以下简称<报告>).该<报告>监测了我国固定宽带的IPv6普 ...

  5. [转帖]22个必须学习的Linux安全命令

    22个必须学习的Linux安全命令 http://os.51cto.com/art/201808/581401.htm Linux系统的安全性涉及很多方面,从设置帐户到确保用户合法,限制比完成工作所需 ...

  6. [转帖学习]Howto Shrink a Thin Provisioned Virtual Disk (VMDK)

    Howto Shrink a Thin Provisioned Virtual Disk (VMDK) Posted by fgrehl on November 24, 2014Leave a com ...

  7. 【转载】JSP 获取真实IP地址的代码

    JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.   但是在通过了 Apache,Squid ...

  8. Windows平台下在服务中添加MySQL

    widows下查看服务 1.桌面计算机-->右键-->管理-->计算机管理(本地)--->服务和应用程序-->服务 2.运行 中输入 services.msc 在服务中添 ...

  9. mysql查看表中列信息

    查看所有数据库中所有表的数据库名和表名 SELECT `TABLES`.`TABLE_SCHEMA`, `TABLES`.`TABLE_NAME` FROM `information_schema`. ...

  10. Java多线程相关的

    很多小伙伴在学习Java的时候,总是感觉Java多线程在实际的业务中很少使用,以至于不会花太多的时间去学习,技术债不断累积!等到了一定程度的时候对于与Java多线程相关的东西就很难理解,今天需要探讨的 ...