#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. 404 Note Found 团队会议纪要

    目录 团队会议 会议纪要1 会议纪要2 会议纪要3 会议纪要4 会议纪要5 会议纪要6 团队会议 会议纪要1 会议纪要2 会议纪要3 会议纪要4 会议纪要5 会议纪要6

  2. RabbitMQ使用笔记

    一.安装 1.下载所需安装包 下载服务端(原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的):http://www.rabbitmq.com/install-windows.htm ...

  3. Beta阶段——3

    一.提供当天站立式会议照片一张: 二. 每个人的工作 (有work item 的ID) (1) 昨天已完成的工作: 今天主要是对管理员功能进行改进,解决了Alpha阶段出现的一些问题 (2) 今天计划 ...

  4. paperOne基于java web的简易四则运算出题网站

    项目成员:张金生     张政 需求概要 1.运算数均为正整数 2.包含的运算符有+,-,*,/ 3.除法运算结果为整除运算 4.批量生成题目并判题 核心功能分析 1.题目生成——java后端 题目生 ...

  5. php推送

    需求: 我想做个会员站内通知的功能.不想用以前的ajax查询,听说有个推技术.以下文章介绍的不错,来自转载, ============================================= ...

  6. Ubuntu 14.04(64bit)使用indicator-sysmonitor显示系统运行状态

    原帖位置:http://tieba.baidu.com/p/3005287033 在使用ubutu时,如果可以查看当前系统使用情况,如CPU,内存,网速等是非常爽的,今天就讲一下一个系统运行状态显示软 ...

  7. getcontext makecontext setcontext swapcontext介绍

    ucontext簇函数学习 https://github.com/zfengzhen/Blog/blob/master/article/ucontext%E7%B0%87%E5%87%BD%E6%95 ...

  8. UVA10054_The Necklace

    很简单,求欧拉回路.并且输出. 只重点说一下要用栈来控制输出. 为啥,如图: 如果不用栈,那么1->2->3->1就回来了,接着又输出4->5,发现这根本连接不上去,所以如果用 ...

  9. 02.基于IDEA+Spring+Maven搭建测试项目--详细过程

    一.背景介绍 1.1公司相关技术 Git:是一款免费的开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,方便多人集成开发 Maven:是基于项目对象模型(POM),可以通过一小段描述信息 ...

  10. Java 信号量 Semaphore 介绍

       Semaphore当前在多线程环境下被扩放使用,操作系统的信号量是个很重要的概念,在进程控制方面都有应用.Java 并发库 的Semaphore 可以很轻松完成信号量控制,Semaphore可以 ...