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《白头吟》两汉:卓文君
白头吟_古诗文网_解析_鉴赏_赏析 白头吟 两汉:卓文君 白头吟:乐府<楚调曲>调名.据<西京杂记>卷三载,蜀地巨商卓王孙的女儿卓文君,聪明美丽,有文采,通音乐.孀居在家时,与 ...
随机推荐
- Java实现对文件的上传下载操作
通过servlet,实现对文件的上传功能 1.首先创建一个上传UploadHandleServlet ,代码如下: package me.gacl.web.controller; import jav ...
- img标签块状与内联的博弈
新手,请前辈们不吝赐教 说到html中img标签是内联还是块状元素,我们首先要知道什么是内联(inline),什么又是块状(block)? 我也在网上查看了一些别人分享的经验,有一个讲到了文档流的概念 ...
- php生成不重复随机字符串
使用时间戳作为原始字符串,再随机生成五个字符随机插入任意位置,生成新的字符串,保证不重复 function rand($len) { $chars='ABCDEFGHIJKLMNOPQRSTUVWXY ...
- iBatis 的删除一条记录
Student.xml 设置删除参数的类型,可以是一个对象的 <delete id="delStudent" parameterClass="int" & ...
- JAVA除去制定字符的方法
只需调用replaceAll()方法: public class Test { public static void main(String[] args) { String s= "abc ...
- webpack学习笔记一(入门)
webpack集成了模块加载和打包等功能 ,这两年在前端领域越来越受欢迎.平时一般是用requirejs.seajs作为模块加载用,用grunt/gulp作为前端构建.webpack作为模块化加载兼容 ...
- ExtJS4 动态加载
由于有人说不要每次都调用ext-all.js,会影响性能,所以有考虑动态加载,动态加载时页面调用ext.js(4.0.7在调试时可考虑用ext-dev.js),然后在onReady之前调用 Ext.L ...
- meta里面的viewport属性
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1, ...
- Android安装 sdk+jdk+Eclipse+Adt开发工具
根据别人提供的手册和安装过程体验加以更新和详细描述 安装Android开发工具 开发Android应用程序的门坎并不高,因为Google已经为Android应用程序开发提供了免费而且跨平台的集成开发环 ...
- 最大乘积(Maximum Product,UVA 11059)
Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...