IOI 2009:Mecho
IOI2009 Mecho
This problem will be judged on SPOJ. Original ID: CTOI09_1
64-bit integer IO format: %lld Java class name: Main
Mecho the bear has found a little treasure - the bees' secret honeypot, which is full of honey! He was happily eating his newfound treasure until suddenly one bee saw him and sounded the bee alarm. He knows that at this very moment hordes of bees will emerge from their hives and start spreading around trying to catch him. He knows he has to leave the honeypot and go home quickly, but the honey is so sweet that Mecho doesn't want to leave too soon. Help Mecho determine the latest possible moment when he can leave.
Mecho's forest is represented by a square grid of
by
unit cells, whose sides are parallel to the north-south and east-west directions. Each cell is occupied by a tree, by a patch of grass, by a hive or by Mecho's home. Two cells are considered adjacent if one of them is immediately to the north, south, east or west of the other (but not on a diagonal). Mecho is a clumsy bear, so every time he makes a step, it has to be to an adjacent cell. Mecho can only walk on grass and cannot go through trees or hives, and he can make at most
steps per minute. At the moment when the bee alarm is sounded, Mecho is in the grassy cell containing the honeypot, and the bees are in every cell containing a hive (there may be more than one hive in the forest). During each minute from this time onwards, the following events happen in the following order:
- If Mecho is still eating honey, he decides whether to keep eating or to leave. If he continues eating, he does not move for the whole minute. Otherwise, he leaves immediately and takes up to
steps through the forest as described above. Mecho cannot take any of the honey with him, so once he has moved he cannot eat honey again. - After Mecho is done eating or moving for the whole minute, the bees spread one unit further across the grid, moving only into the grassy cells. Specifically, the swarm of bees spreads into every grassy cell that is adjacent to any cell already containing bees. Furthermore, once a cell contains bees it will always contain bees (that is, the swarm does not move, but it grows).
In other words, the bees spread as follows: When the bee alarm is sounded, the bees only occupy the cells where the hives are located. At the end of the first minute, they occupy all grassy cells adjacent to hives (and still the hives themselves). At the end of the second minute, they additionally occupy all grassy cells adjacent to grassy cells adjacent to hives, and so on. Given enough time, the bees will end up simultaneously occupying all grassy cells in the forest that are within their reach. Neither Mecho nor the bees can go outside the forest. Also, note that according to the rules above, Mecho will always eat honey for an integer number of minutes. The bees catch Mecho if at any point in time Mecho finds himself in a cell occupied by bees.
Task
Write a program that, given a map of the forest, determines the largest number of minutes that Mecho can continue eating honey at his initial location, while still being able to get to his home before any of the bees catch him.
Constraints
- the size (side length) of the map
- the maximum number of steps Mecho can take in each minute
Input
The input contains several testcases.
The fist line contains the number of testcase T.
Each testcase has the form as following:
- The first line contains the integers
and
, separated by a space. - The next
lines represent the map of the forest. Each of these lines contains
characters with each character representing one unit cell of the grid. The possible characters and their associated meanings are as follows:
T denotes a tree G denotes a grassy cell M denotes the initial location of Mecho and the honeypot, which is also a grassy cell D denotes the location of Mecho's home, which Mecho can enter, but the bees cannot. H denotes the location of a hive
Note: It is guaranteed that the map will contain exactly one letter M, exactly one letter D and at least one letter H. It is also guaranteed that there is a sequence of adjacent letters Gthat connects Mecho to his home, as well as a sequence of adjacent letters G that connects at least one hive to the honeypot (i.e., to Mecho's initial location). These sequences might be as short as length zero, in case Mecho's home or a hive is adjacent to Mecho's initial location. Also, note that the bees cannot pass through or fly over Mecho's home. To them, it is just like a tree.
Output
For each test , your program must write to standard output a single line containing a single integer: the maximum possible number of minutes that Mecho can continue eating honey at his initial location, while still being able to get home safely. If Mecho cannot possibly reach his home before the bees catch him, the number your program writes to standard output must be
instead.
Grading
For a number of tests, worth a total of 40 points,
will not exceed 60.
Example
For the input data:
1
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT
the correct result is:
1
Explanation of the example: After eating honey for one minute, Mecho can take the shortest path directly to the right and he will be home in another two minutes, safe from the bees.
For the input data:
1
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT
the correct result is:
2
Explanation of the example: After eating honey for two minutes, Mecho can take steps
during the third minute, then steps
during the fourth minute and steps
during the fifth minute.
Source
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int INF=;
char map[][];
int dis[][];
int Time[][];
int n,k;
struct Data{
int x,y;
}S;
void P()
{
queue<Data>q;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(map[i][j]=='H')
q.push((Data){i,j});
else if(map[i][j]!='T')
dis[i][j]=INF; while(!q.empty())
{
Data node=q.front();q.pop();
if((map[node.x+][node.y]=='G'||map[node.x+][node.y]=='M')
&&dis[node.x+][node.y]>dis[node.x][node.y]+)
dis[node.x+][node.y]=dis[node.x][node.y]+,q.push((Data){node.x+,node.y});
if((map[node.x-][node.y]=='G'||map[node.x-][node.y]=='M')
&&dis[node.x-][node.y]>dis[node.x][node.y]+)
dis[node.x-][node.y]=dis[node.x][node.y]+,q.push((Data){node.x-,node.y});
if((map[node.x][node.y+]=='G'||map[node.x][node.y+]=='M')
&&dis[node.x][node.y+]>dis[node.x][node.y]+)
dis[node.x][node.y+]=dis[node.x][node.y]+,q.push((Data){node.x,node.y+});
if((map[node.x][node.y-]=='G'||map[node.x][node.y-]=='M')
&&dis[node.x][node.y-]>dis[node.x][node.y]+)
dis[node.x][node.y-]=dis[node.x][node.y]+,q.push((Data){node.x,node.y-});
}
return;
} bool DFS(int t)
{
if(t>=dis[S.x][S.y])return false;
queue<Data>q;
q.push((Data){S.x,S.y});
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
Time[i][j]=INF;
Time[S.x][S.y]=t*k+; while(!q.empty())
{
Data node=q.front();q.pop();
int nt=Time[node.x][node.y];
if(dis[node.x+][node.y]>nt/k&&Time[node.x+][node.y]>nt+){
Time[node.x+][node.y]=nt+;q.push((Data){node.x+,node.y});
if(map[node.x+][node.y]=='D')
return true;
} if(dis[node.x-][node.y]>(nt)/k&&Time[node.x-][node.y]>nt+){
Time[node.x-][node.y]=nt+,q.push((Data){node.x-,node.y});
if(map[node.x-][node.y]=='D')
return true;
} if(dis[node.x][node.y+]>(nt)/k&&Time[node.x][node.y+]>nt+){
Time[node.x][node.y+]=nt+,q.push((Data){node.x,node.y+});
if(map[node.x][node.y+]=='D')
return true;
} if(dis[node.x][node.y-]>(nt)/k&&Time[node.x][node.y-]>nt+){
Time[node.x][node.y-]=nt+,q.push((Data){node.x,node.y-});
if(map[node.x][node.y-]=='D')
return true;
}
}
return false;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%s",map[i]+); for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(map[i][j]=='M'){
S.x=i;S.y=j;
}
P();
int lo=,hi=;
while(lo<=hi)
{
int mid=(lo+hi)>>;
if(DFS(mid))lo=mid+;
else hi=mid-;
}
printf("%d\n",hi);
return ;
}
IOI 2009:Mecho的更多相关文章
- NOIp 2009:靶形数独
题目描述 Description 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向Z 博士请教, Z ...
- DDoS攻防战(一):概述
原文出处: 陶辉的博客 欢迎分享原创到伯乐头条 (此图摘自<Web脚本攻击与防御技术核心剖析>一书,作者:郝永清先生) DDoS,即 Distributed Denial of Ser ...
- 对编程语言的需求总结为四个:效率,灵活,抽象,生产率(C++玩的是前三个,Java和C#玩的是后两个)
Why C++ ? 王者归来(转载) 因为又有人邀请我去Quora的C2C网站去回答问题去了,这回是 关于 @laiyonghao 的这篇有点争议的博文<2012 不宜进入的三个技术点>A ...
- Spark:大数据的电花火石!
什么是Spark?可能你很多年前就使用过Spark,反正当年我四六级单词都是用的星火系列,没错,星火系列的洋名就是Spark. 当然这里说的Spark指的是Apache Spark,Apache Sp ...
- zz 【见闻八卦】《金融时报》年度商业书单:互联网题材占一半
[见闻八卦]<金融时报>年度商业书单:互联网题材占一半 文 / 见闻学堂 2014年12月18日 09:47:38 0 中国最好的金融求职培训:见闻学堂(微信号:top-elites) ...
- DDoS攻防战 (一) : 概述
岁寒 然后知松柏之后凋也 ——论语·子罕 (此图摘自<Web脚本攻击与防御技术核心剖析>一书,作者:郝永清先生) DDoS,即 Distributed Denial of Servi ...
- 转发 DDoS攻防战 (一) : 概述
岁寒 然后知松柏之后凋也 岁寒 然后知松柏之后凋也 ——论语·子罕 (此图摘自<Web脚本攻击与防御技术核心剖析>一书,作者:郝永清先生) DDoS,即 Distributed ...
- NO.009-2018.02.14《临江仙·送钱穆父》宋代:苏轼
临江仙·送钱穆父_古诗文网 临江仙·送钱穆父 宋代:苏轼 一别都门三改火,天涯踏尽红尘.依然一笑作春温.无波真古井,有节是秋筠.自从我们在京城分别一晃又三年,远涉天涯你奔走辗转在人间.相逢一笑时依然像 ...
- NO.007-2018.02.12《白头吟》两汉:卓文君
白头吟_古诗文网_解析_鉴赏_赏析 白头吟 两汉:卓文君 白头吟:乐府<楚调曲>调名.据<西京杂记>卷三载,蜀地巨商卓王孙的女儿卓文君,聪明美丽,有文采,通音乐.孀居在家时,与 ...
随机推荐
- Css实现透明效果,兼容IE8
Css实现透明效果,兼容IE8 >>>>>>>>>>>>>>>>>>>>> ...
- python-字典(第二篇(四):字典)
[Python之旅]第二篇(四):字典 摘要: 说明: 显然Python中字典的学习过程与列表是一样的,主要是围绕下面的函数来进行重点学习: 1 2 3 4 5 6 7 8 9 10 11 & ...
- 点击其它地方隐藏div/事件冒泡/sweet-alert阻止冒泡
点击document时把div隐藏,但点击div时阻止点击事件冒泡到document,从而实现“点击文档其它地方隐藏div,点击div本身不隐藏”.js代码如下:$("#div") ...
- window.showModalDialog 子窗口和父窗口不兼容最新的谷歌
最新版的谷歌不支持window.showModalDialog的写法,会出现,找不到方法的问题,同时返回值的方法window.dialogArguments;也用不了. 这里就只能用最原版的windo ...
- Android开发需要注意的地方
1.理解运用商场概略 开发者对商场状况的理解与APP的胜利紧密相连,往常,AppStore和GooglePlay能够说是挪动运用最为丰厚的运用生态,像苹果的下载计算表单会记载抢手运用的下载 ...
- 监听polygon变化
Polygons are made of Paths which are just MVCArrays (in this case, they're a list of LatLng objects) ...
- string.Join和Reverse的简单使用示例
String.Join 方法 (String, String[]) 串联字符串数组的所有元素,其中在每个元素之间使用指定的分隔符. 例如,如果 separator 为“,”且 value 的元素为“a ...
- C# 通过hessian调Java注意事项
照理说C#可以通过标准的web服务可以轻松地调用Java,但是鉴于hessian的高性能及开发效率,个人认为C#通过hessian调用java是很值得提倡的.之前完成的一个比较大型的企业应用项目就是采 ...
- MySQL性能测试工具之mysqlslap使用详解
mysqlslap是mysql自带的基准测试工具,优点:查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新,给出了性能测试数据而且提供了多种引擎的性能比较.msq ...
- javascript社交平台分享-新浪微博、QQ微博、QQ好友、QQ空间、人人网
整理的五个社交平台的分享 <!doctype html> <html lang="en"> <head> <meta charset=&q ...