BFS:Meteor Shower(POJ 3669)

题目大意:这只Bessie的牛又要来闹事了,这次她的任务就是来躲流星雨,流星雨就是在一定时间会从天上砸到Bessie的所在的正方形区域内(Bessie在0,0的位置),然后砸下来后流星雨会把区域内的上下左右区域全部砸烂,并且砸烂后Bessie不能再到这里,Bessie也不能在流星雨打击的地方呆着(也就是只能在流星雨要下来的时间之前才能在这里),问你Bessie要走多少步才能到安全区域
这一道题好玩,我们这样想,Bessie走过的路就是等值的(也就是每走一步都是1),而且还要要求你在矩阵内走,那么这一题很明显就是要你用BFS了
但是关键的问题是,陨石不是一下子全部掉落下来的,而是有时间间隔的,那有这么一个思路,我们可以在一定区域时间内看牛能走到哪里,然后下一波流星雨,毁掉能走掉的部分区域,然后在下一波,这样我们只用保存最小的那一些路就可以了,但是这样的话显然是不现实的,因为有时候流星雨砸很长时间,但是一直砸不到某个区域,我们做的很多工作都是白费的,交题绝对TLE,不要问我为什么知道
所以我们要换一种思路,既然流星雨都会砸下来,那么我们为什么不直接先假设让流星雨先全部砸下来看剩下多少安全区域呢???(这是一种很好的思想,以后会经常用到),但是因为流星雨砸下来是有时间区分的,所以我们要保留流星雨砸下来的时间,然后从0,0开始BFS,如果BFS所用的步数不足以到达安全区域,那么就判断无法到达,如果能到到安全区域,那么第一次到达就肯定是最短的距离了(BFS的特性),
另外,还要注意,我们每次扫过位置以后,一定要对位置进行标记,可以使用known区域,但是一定要注意,一定要把四个方位区域全部标记上,而不是只标记出队的那个点,不这样做的话,重复会非常多,会TLE,或者直接把map写上当前最短距离,然后进行判断就可以了
另外一个小问题,原来我一直搞错了memset的工作原理,原来这玩意是按ANSCII来填字节的。。。。
#include <iostream>
#include <functional>
#include <algorithm>
#include <queue>
#define MAX_N 310
#define MIN(a,b) a<b?a:b using namespace std; void Search(const int);
void Inivilize_dir(void); pair<int, int>dir[];
static int dp[MAX_N + ][MAX_N + ];
static int map[MAX_N + ][MAX_N + ]; void Inivilize_dir(void)
{
dir[].first = -; dir[].second = ;
dir[].first = ; dir[].second = ;
dir[].first = ; dir[].second = -;
dir[].first = ; dir[].second = ;
} int main(void)
{
int M, tx, ty, drop_time,ttx, tty;
Inivilize_dir();
while (~scanf("%d", &M))
{
memset(map, -, sizeof(map)); for (int i = ; i < M; i++)
{
scanf("%d%d%d", &tx, &ty, &drop_time);
if (map[tx][ty] != -)
map[tx][ty] = MIN(map[tx][ty], drop_time);//最早炸毁时间
else map[tx][ty] = drop_time;
for (int j = ; j < ; j++)//周围四个点都要
{
ttx = tx + dir[j].first; tty = ty + dir[j].second;
if (ttx >= && ttx <= MAX_N
&& tty >= && tty <= MAX_N//坐标在矩阵内
)
if (map[ttx][tty] != -)
map[ttx][tty] = MIN(map[ttx][tty], drop_time);
else map[ttx][tty] = drop_time;
}
}
if (map[][] == )
cout << - << endl;
else if (map[][] == -)//开始都打不到还考虑什么,不动
cout << << endl;
else Search(M);
}
return ;
} void Search(const int M)
{
int ttx, tty, tx, ty;
queue<pair<int, int>>que;
pair<int, int>tmp, d_tmp;
que.push(tmp);
while (!que.empty())
{
tmp = que.front(); que.pop();
tx = tmp.first; ty = tmp.second; for (int j = ; j < ; j++)
{
ttx = tx + dir[j].first; tty = ty + dir[j].second;
if (ttx >= && ttx <= MAX_N
&& tty >= && tty <= MAX_N//坐标在矩阵内
)
{
if (map[ttx][tty] == -)//如果找到安全位置,则更新最短距离
{
cout << dp[tx][ty] + << endl;
return;
}
if (dp[tx][ty] + <= map[ttx][tty] - )//如果都比这个时间大,那肯定被炸死了
{
dp[ttx][tty] = dp[tx][ty] + ;
d_tmp.first = ttx; d_tmp.second = tty;
que.push(d_tmp);//还在时间之内,入队
map[ttx][tty] = dp[ttx][tty];
//不能用known去标记入口,除非是标记所有的点
}
}
}
}
cout << - << endl;
}
BFS:Meteor Shower(POJ 3669)的更多相关文章
- Meteor Shower POJ - 3669 (bfs+优先队列)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26455 Accepted: 6856 De ...
- POJ 3669 Meteor Shower【BFS】
POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...
- 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(bfs)
-->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...
- POJ 3669 Meteor Shower BFS求最小时间
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31358 Accepted: 8064 De ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- poj 3669 Meteor Shower
Me ...
随机推荐
- Yii2 radioList设置默认值
可以在对应的Controller的action中设置 $model->type = 1; 在view中 <?php $form = ActiveForm::begin(); ?> ...
- Windows python3.3下安装BeautifulSoup
首先在官网下载:http://www.crummy.com/software/BeautifulSoup/#Download BeautifulSoup在版本4以上都开始支持python3了,所以就下 ...
- 在Ubuntu下安装*.sh
在Ubuntu下安装*.sh和*.bin的方法 [日期:2009-12-07] 来源:Linux公社 作者:Linux编辑 [字体:大 中 小] 记下在Ubuntu下安装*.sh和*.bin的简 ...
- SpringMVC 配置log4j
log4j.properties相关 http://www.cnblogs.com/ITEagle/archive/2010/04/23/1718365.html http://www.cnblogs ...
- 【poj1236】 Network of Schools
http://poj.org/problem?id=1236 (题目链接) 题意 给定一个有向图,求:1.至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点:2.至少要加多少条边,才能使得从任 ...
- cogs896 圈奶牛
描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. PROGRAM NAM ...
- DALSA Coreco - 图像处理软件(Sapera LT )
http://blog.csdn.net/linglongyouzhi/article/details/3505845 概述 Sapera LT 是一套用于图像采集.显示和控制的独立于硬件以外的 C ...
- HBase概念学习(十)HBase与MongDB等NoSQL数据库对照
转载请注明出处: jiq•钦's technical Blog - 季义钦 一.开篇 淘宝之前使用的存储层架构一直是MySQL数据库,配合以MongDB,Tair等存储. MySQL因为开源,而且生态 ...
- MySQL------如何安装mysql-connector-java-5.1.38.zip
下载地址:http://dev.mysql.com/downloads/connector/j/ 安装mysql-connector-java-5.1.38.zip:1.解压文件->把里面的my ...
- Laravel教程 六:表单 Forms
Laravel教程 六:表单 Forms 此文章为原创文章,未经同意,禁止转载. Form laravel 5.2 之后请使用 laravelcollective/html 替换 illuminate ...