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《白头吟》两汉:卓文君
白头吟_古诗文网_解析_鉴赏_赏析 白头吟 两汉:卓文君 白头吟:乐府<楚调曲>调名.据<西京杂记>卷三载,蜀地巨商卓王孙的女儿卓文君,聪明美丽,有文采,通音乐.孀居在家时,与 ...
随机推荐
- Python CMDB开发
Python CMDB开发 运维自动化路线: cmdb的开发需要包含三部分功能: 采集硬件数据 API 页面管理 执行流程:服务器的客户端采集硬件数据,然后将硬件信息发送到API,API负责将获取 ...
- codevs 1519 过路费 最小生成树+倍增
/*codevs 1519 过路费 最小生成树+倍增*/ #include<iostream> #include<cstdio> #include<cstring> ...
- 上机实践 - - 一个例子了解C/C++中指针与数组的区别
本例子来自于<剑指Offer>(P37) 解答如下: size1:20 data1是一个数组,sizeof(data1)是求数组大小. 这个数组包含5个整数,每个整数4个字节,共20字节. ...
- NC V6 nchome文件目录及其作用介绍
NC V6发布一段时间了,各个NC6.0 nchome文件夹下各个子文件夹内容和作用 ant:存放Apache Ant,用来执行EJB的构建. bin: 存放nc部署和系统监控等命令.configsy ...
- hive hbase pig 区别
参考文档http://www.linuxidc.com/Linux/2014-03/98978.htm
- WCF 生产json对外的接口
调用wcf public ActionResult Index() { ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序."; WC ...
- 实现Android K的伪沉浸式
在Android 5.0之后引入了MD风格,从而状态栏沉浸也成为了一种设计习惯.而停留在之Android L之前的Android系统则不能直接实现沉浸式,这里就介绍一下如何实现Android K系列的 ...
- 1.PhotoShop缩小图片的三种方式
先声明,本人不是高前端的,若有不当或者不合理的地方,还望前端爱好者多多指教.此处只是留作个人记录备忘. PS中有三种缩小 1.视图缩小,那方法很多缩放工具.Ctrl+"-",导航器 ...
- 一种实现C++反射功能的想法(三)
如何实现类型名跟类型的对应, 我们很容易想到map, 没错, 就是使用map实现的. std::map<std::string, .....>, 等下, 第二部分该填什么类型, 一个函数指 ...
- windows 下PHP 和 nginx配置
http://www.cnblogs.com/huayangmeng/archive/2011/06/15/2081337.html