POJ 3669

去看流星雨,不料流星掉下来会砸毁上下左右中五个点。每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少?

思路:对流星雨排序,然后将地图的每个点的值设为该点最早被炸毁的时间

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std; #define INDEX_MAX 512
int map[INDEX_MAX][INDEX_MAX];
bool visited[INDEX_MAX][INDEX_MAX];
struct Meteor
{
int x, y, t;
};
typedef Meteor P; Meteor m[50008];
int n; const int direction[5][2] = {
{ -1, 0 },
{ 1, 0 },
{ 0, -1 },
{ 0, 1 },
{ 0, 0 },
}; int last; int bfs()
{
memset(visited, 0, sizeof(visited));
queue<P> que;
P current;
current.x = 0;
current.y = 0;
// 当前花费时间
current.t = 0;
que.push(current);
while (que.size())
{
// 做个备份
const P p = que.front(); que.pop();
for (int j = 0; j < 4; ++j)
{
current = p;
current.x = current.x + direction[j][0];
current.y = current.y + direction[j][1];
++current.t; if (current.x >= 0 && current.y >= 0 && map[current.x][current.y] > current.t && !visited[current.x][current.y])
{
visited[current.x][current.y] = true;
// 爆炸时间大于当前时间,是安全的
if (map[current.x][current.y] > last)
{
// 当前位置爆炸时间大于流星雨最晚落下的时间,说明跑出了流星雨区域
return current.t;
}
que.push(current);
}
}
} return -1;
} int main()
{
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> m[i].x >> m[i].y >> m[i].t;
} // 地图中每个点的值表示最早在什么时候被炸毁
memset(map, 0x7F, sizeof(map));
for (int i = 0; i < n; ++i)
{
last = max(last, m[i].t);
for (int j = 0; j < 5; ++j)
{
int nx = m[i].x + direction[j][0];
int ny = m[i].y + direction[j][1];
if (nx >= 0 && ny >= 0 && map[nx][ny] > m[i].t)
{
map[nx][ny] = m[i].t;
}
}
}
if (map[0][0] == 0)
{
cout << -1 << endl;
}
else
{
cout << bfs() << endl;
}
return 0;
}

POJ 3669 Meteor Shower【BFS】的更多相关文章

  1. poj 3669 Meteor Shower(bfs)

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

  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(流星雨)

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

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

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

  6. poj 3669 Meteor Shower

                                                                                                      Me ...

  7. 【POJ 3669 Meteor Shower】简单BFS

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

  8. POJ 3669 Meteor Shower BFS 水~

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

  9. poi 3669 meteor shower (bfs)

    题目链接:http://poj.org/problem?id=3669 很基础的一道bfs的题,然而,我却mle了好多次,并且第二天才发现错在了哪里_(:з)∠)_ 写bfs或者dfs一定要记得对走过 ...

随机推荐

  1. C# GetType与typeof

    在反射和泛型中经常会使用到Type类,获取Type的最常用的方法是 obj.GetType(),和typeof(T).在获取泛型的type时有些小坑. public static void Main( ...

  2. webpack踩坑之路——构建基本的React+ES6项目

    转自:http://www.cnblogs.com/ghost-xyx/p/5483464.html webpack是最近比较火的构建工具,搭配上同样比较火的ReacJS与ES6(ES2015)一定是 ...

  3. 第一章 Shiro简介——《跟我学Shiro》(转)

    目录贴:跟我学Shiro目录贴 1.1  简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可 ...

  4. juery学习6——焦点事件

    参考资料 深入理解javascript中的焦点管理:http://www.cnblogs.com/xiaohuochai/p/5874447.html

  5. R自动数据收集第二章HTML笔记1(主要关于handler处理器函数和帮助文档所有示例)

    本文知识点:     1潜在畸形页面使用htmlTreeParse函数 2startElement的用法 3闭包 4handler函数的命令和函数体主要写法 5节点的丢弃,取出,取出标签名称.属性.属 ...

  6. python标准模块(os及sys模块)

    一.os模块 用于提供系统级别的操作 os.getcwd() 获取当前工作目录 os.stat('path/filename') 获取文件/目录信息,其中包括文件大小等 os.sep 获得操作系统特定 ...

  7. 调用WebServices超时

    1. 服务器端设置超时 在 web.config 的 system.web 里添加如下配置项: < httpRuntimeexecutionTimeout="300000"/ ...

  8. XSS(跨站脚本攻击)的最全总结

    从OWASP的官网意译过来,加上自己的理解,算是比较全面的介绍.有兴趣的可私下交流. XSS 跨站脚本攻击 ============================================== ...

  9. jcFeather For Arnold

    jcFeather 现在可以支持Arnold了,可以用Arnold来贴图方式渲染jcFeather的刷出的多边形羽毛. jcFeather 自带笔刷刷羽毛多边形,再配上一个Arnold shader ...

  10. Java开源库

    Java SE 7 API Docs from Oracle Apache IO库操作IO与文件 2.4 XML4j 1.6.1 Json.org google-json 2.5 WindowBuil ...