奔跑吧,傻牛

  题目大意:这只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)的更多相关文章

  1. Meteor Shower POJ - 3669 (bfs+优先队列)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26455   Accepted: 6856 De ...

  2. POJ 3669 Meteor Shower【BFS】

    POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...

  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 (BFS+预处理)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  5. 题解报告:poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  6. 【POJ - 3669】Meteor Shower(bfs)

    -->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...

  7. POJ 3669 Meteor Shower BFS求最小时间

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31358   Accepted: 8064 De ...

  8. POJ 3669 Meteor Shower(流星雨)

    POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears ...

  9. poj 3669 Meteor Shower

                                                                                                      Me ...

随机推荐

  1. POJ2288 Islands and Bridges

    Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...

  2. Erlang练习-UDP

    贴一下代码,例子是从别人那里直接抄来的: -module(myudp). -export([start/0, client/1]). %% Server start() -> spawn(fun ...

  3. java导出txt文本

    页面 项目结构 html代码 <html> </head> <body> <form action="down/downLoad" met ...

  4. SGU 275 To xor or not to xor

    time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard output: standard The ...

  5. 在 ASP.NET MVC 3 中应用 KindEditor

    http://www.cnblogs.com/weicong/archive/2012/03/31/2427608.html 第一步 将 KindEditor 的源文件添加到项目中,建议放到 /Scr ...

  6. 深入理解 Javascript 面向对象编程

    一:理解构造函数原型(prototype)机制 prototype是javascript实现与管理继承的一种机制,也是面向对象的设计思想.构造函数的原型存储着引用对象的一个指针,该指针指向与一个原型对 ...

  7. 锋利的jQuery-4--$(document).ready()和window.onload方法的区别

    jQuery中的$(document).ready()和JavaScript中的window.onload方法主要有两个方面的不同: 1.执行时机: onload : 网页中所有的元素和元素的关联文件 ...

  8. <span>和<a>的margin上下和padding上下不起作用的原因和解决

    使用到了<span>和<a>标签,发现在样式里面直接写margin-top.margin-bottom和padding-top.padding-bottom都不起作用,页面样式 ...

  9. linux读写ntfs

    frankly speaking, i hope to get a higher salary. yours frankly= yours sincerely = sincerely yours =y ...

  10. 将DataSet(DataTable)转换成JSON格式(生成JS文件存储)

    public static string CreateJsonParameters(DataTable dt) { /**/ /**/ /**/ /* /*********************** ...