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. html5中新增的form表单属性

    html5中新增两个表单属性,分别autocomplete和novalidate属性 1.autocomplete属性 该属性用于控制自动完成功能的开启和关闭.可以设置表单或者input元素,有两个属 ...

  2. 美团HD(3)-加载分类导航数据

    DJHomeViewController.m /** 设置导航栏左侧内容 */ - (void)setupLeftNavItem { // Logo UIImageView *logoView = [ ...

  3. Apache DdlUtils入门

    Introduction  DdlUtils is a small, easy-to-use component for working with Database Definition (DDL) ...

  4. MySQLdb的一些经验

    遇到过的几类问题: 如果保持长连接,即使在mysql数据库默认的connection timeout内,也有可能出现"mysql server has gone away".还有另 ...

  5. Ubuntu 14.04开发环境初始化

    安装fcitx, fcitx-googlepinyin, 移除默认键盘快捷键. 英文版不要安装系统推荐的语言更新,会使浏览器以及其他的应用的字体变成bitmap. 安装nvidia驱动 安装vim,设 ...

  6. php数组array_push()和array_pop()以及array_shift()函数

    <?php /** * array_push()将一个或多个单元压入数组的末尾(入栈) */ $stack = array("Java", "Php", ...

  7. 编程轶事-java中的null-遁地龙卷风

    1.null是个奇妙的东西,可以理解为对象占位符 User user = null; System.out.println(user.getCredits()); 可以通过编译, User user; ...

  8. 答辩HTML5

    答辩有三个项目,有三个游戏和知乎,游戏都是有js写的,我想说的是想要做一个是那么难啊!老师给了我们游戏的项目还有游戏的思路构成,完成项目.还有一个知乎,也很难,用到HTML,css3,php,数据库, ...

  9. 转载:gulp文件

    这是我的文件目录结构图  下面是我gulpfile.js的配置 'use strict' var gulp=require('gulp'); var gutil=require('gulp-util' ...

  10. tomcat 快速部署静态文件

    server.conf配置: <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software ...