POJ 3669 Meteor Shower【BFS】
去看流星雨,不料流星掉下来会砸毁上下左右中五个点。每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少?
思路:对流星雨排序,然后将地图的每个点的值设为该点最早被炸毁的时间
#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】的更多相关文章
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- POJ 3669 Meteor Shower BFS求最小时间
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31358 Accepted: 8064 De ...
- poj 3669 Meteor Shower
Me ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- POJ 3669 Meteor Shower BFS 水~
http://poj.org/problem?id=3669 题目大意: 一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达 ...
- poi 3669 meteor shower (bfs)
题目链接:http://poj.org/problem?id=3669 很基础的一道bfs的题,然而,我却mle了好多次,并且第二天才发现错在了哪里_(:з)∠)_ 写bfs或者dfs一定要记得对走过 ...
随机推荐
- std::unique_lock<std::mutex> or std::lock_guard<std::mutex> C++11 区别
http://stackoverflow.com/questions/20516773/stdunique-lockstdmutex-or-stdlock-guardstdmutex The diff ...
- DX系列之TreeList
参考资料: DevXpress控件: 第三篇: 将 父子 关系进行到底
- 一行神奇的javascript代码
写本篇文章的缘由是之前群里@墨尘发了一段js代码,如下: (!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~ ...
- [Machine Learning] logistic函数和softmax函数
简单总结一下机器学习最常见的两个函数,一个是logistic函数,另一个是softmax函数,若有不足之处,希望大家可以帮忙指正.本文首先分别介绍logistic函数和softmax函数的定义和应用, ...
- Java开源库
Java SE 7 API Docs from Oracle Apache IO库操作IO与文件 2.4 XML4j 1.6.1 Json.org google-json 2.5 WindowBuil ...
- HTTP 错误 500.21 - Internal Server Error 解决方案
不久前重新安装了Windows7,在安装了VS2010 开发平台之后,将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序“Ni ...
- WebService入门
1.什么是web服务: web服务是一种可以用来解决跨网络应用集成问题的开发模式,这种模式为实现"软件即服务"提供了技术保障. 2.web服务的三个核心 2.1 SOAP SOA ...
- .net mvc中的表单异步提交
// // 摘要: // 将 <form> 开始标记写入响应. // // 参数: // ajaxHelper: // AJAX 帮助器. // // actionName: // 将处理 ...
- jvm--2.类加载机制
3.JVM类加载机制 (1)类加载机制 虚拟机把描述类的数据从Class文件,用ClassLoader ,加载到内存,并对数据进行校验.转换解析和初始化,最终形成虚拟机直接使用的java类型, 这就是 ...
- break与continue的区别
break 在while.for.do...while.while循环中使用break语句退出当前循环,直接执行后面的代码. continue 的作用是仅仅跳过本次循环,而整个循环体继 ...