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. jQuery选择器总结

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法   $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中 ...

  2. VirtualBox COM对象获取失败

    问题描述: Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but CLSID_VirtualBox w/ IUnknown works. ...

  3. TCP学习之五:客户端、服务端异步传输字符串

    参考学习张子阳大神的博客:http://www.cnblogs.com/JimmyZhang/category/101698.html 消息发送接口: 消息接收接口: 客户端: 服务端: 消息发送类: ...

  4. dom4j的小例子

    1.要解析的xml文件book.xml <?xml version="1.0" encoding="UTF-8"?> <books> & ...

  5. 【学习笔记】C语言之词法规则

    一.字符 标准并没有规定C环境必须使用特定的字符集,但是它规定了字符集必须包含英语所有的大小写字母,数字0到9,以及下面的字符: ! # % ^ & * ( ) _ – + =  / . ? ...

  6. dom解析和sax解析的区别及优缺点

    dom解析一开始就将文档所有内容装入内存,每个元素(标签)都作为一个element对象存储,形成对象树,缺点是对内存占用大,不能解析数据量很大的文档:优点是方便进行crud操作. sax解析,逐行解析 ...

  7. jemter的安装和使用

    1.下载地址:http://jmeter.apache.org/download_jmeter.cgi 2.安装 (1)解压,解压之后如图 (2)配置环境变量 我解压之后的目录是:D:\softwar ...

  8. 移动端视网膜(Retina)屏幕下1px边框线 解决方案

    原因: 因为Retine屏的分辨率始终是普通屏幕的2倍,1px的边框在devicePixelRatio=2的retina屏下会显示成2px. 但在IOS8中,已经支持0.5px了,那就意味着, 在de ...

  9. 安卓ApiDemos最简单的使用方法

    http://download.csdn.net/detail/ffwmxr/7401067#comment 正确使用方法:开新工程, 名字API Demos,将下载文件里的 src,res, And ...

  10. 详解Node解析URL网址

    前提给大家声明一下,我操作的环境是Mac终端下操作的.(前提是你先要下载好node.js) 说道URL 恐怕都不陌生,但是要说URL,就 必须先说下URI URI是统一资源标识符,是一个用于标识某一互 ...