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 ...
随机推荐
- 【CodeForces 621A】Wet Shark and Odd and Even
题 Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wan ...
- [IOS Delegate和协议]
转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/25655443 作者:小马 代理和协议的语法这里不赘述,自己查资料. 这个demo的 ...
- maven运行javaWeb项目
首先从svn下载下来的maven项目,需要点击项目,然后import--->Existing Maven Projects->全选之后点next就转换成功了,然后 run as--> ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- 数组添加:如何往数组的"null"位置插入数据呢?
数组添加,当已经存在的一个数组时,如何往数组的"null"位置插入数据呢? 分析: 1.循环遍历数组元素,找出null的位置(下标) 2.设置一个变量,接收null位置下标值 3. ...
- MVC 返回图片
//调用 http://localhost:60663/home/GetCoder39Img?mycode=123443545 public void GetCoder39Img(string myc ...
- Recruit Coupon Purchase Winner's Interview: 2nd place, Halla Yang
Recruit Coupon Purchase Winner's Interview: 2nd place, Halla Yang Recruit Ponpare is Japan's leading ...
- subplot的应用
import matplotlib.pyplot as Plot Plot.subplot(3, 4, (1, 7)) Plot.subplot(1, 4, 4) Plot.subplot(3, 4, ...
- css3应用之自定义选中文字的背景颜色
在看很多的博客主题时候发现大多数都对选中文字的背景颜色做了相应的处理.其实这样是很符合用户体验的.因为有很多的人会用鼠标选择着一行一行的阅读.其中就包括本人... 浏览器中默认的选中的文字颜色为白色, ...
- [Effective JavaScript 笔记]第16条:避免使用eval创建局部变量
js中的eval函数是一个强大.灵活的工具.强大的工具容易被滥用,所以了解是值得的.(本人只用过它来处理json数据).错误使用eval函数的方式一:允许它干扰作用域.调用eval函数会将其参数作为j ...