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一定要记得对走过 ...
随机推荐
- git rebase
git rebase -i HEAD~[number_of_commits] git rebase -i HEAD~2
- excel工具类
excel工具类 import com.iport.framework.util.ValidateUtil; import org.apache.commons.lang3.StringUtils; ...
- iPhone屏幕尺寸/launch尺寸/icon尺寸
屏幕尺寸 6p/6sp 414 X 736 6/6s 375 X 667 5/5s 320 X 568 4/4s 320 X 480 la ...
- Android手机清除微信缓存
方法一: 1.任意找一个微信好友,给他发送网址 http://debugx5.qq.com 2.自己点击这个网址跳转 3.进入后看到下面的页面,通过勾选第二张截图的Cookie和文件缓存来清除微信缓存 ...
- 浅谈Java中的equals和==(转)
浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...
- 看着水了一天的群,终于看到一段高质量的代码了分享一下localStorage
_history : { //缓存 isLocalStorage:window.localStorage?true:false, set : function(key,value){ //设置缓存 i ...
- new Array(n) 的坑 密集数组和稀疏数组
今天写Vue时遇到一个奇怪问题,需要监控网页上的input 是否都有值 // var blanks = new Array(number); // blanks的监控属性 var emptyNumbe ...
- my97中文乱码问题
在使用my97日期插件后页面显示中文乱码问题: 解决方法: 把下面这段代码复盖到你的ZH-CN.js就解决了 var $lang={ errAlertMsg: "\u4E0D\u5408\u ...
- secureCRT The remote system refused the connection.
转 http://blog.csdn.net/lifengxun20121019/article/details/13627757 我在实践远程登录工具SecureCRT的时候遇到了这个问题 Ubun ...
- Objective-C中的浅拷贝和深拷贝(转载)
本文转自:http://segmentfault.com/blog/channe/1190000000604331 浅拷贝 浅拷贝就是对内存地址的复制,让目标对象指针和源对象指向同一片内存空间.如: ...