IOI2009 Mecho

Time Limit: 10000ms
Memory Limit: 262144KB

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的更多相关文章

  1. NOIp 2009:靶形数独

    题目描述 Description 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向Z 博士请教, Z ...

  2. DDoS攻防战(一):概述

    原文出处: 陶辉的博客   欢迎分享原创到伯乐头条 (此图摘自<Web脚本攻击与防御技术核心剖析>一书,作者:郝永清先生) DDoS,即 Distributed Denial of Ser ...

  3. 对编程语言的需求总结为四个:效率,灵活,抽象,生产率(C++玩的是前三个,Java和C#玩的是后两个)

    Why C++ ? 王者归来(转载) 因为又有人邀请我去Quora的C2C网站去回答问题去了,这回是 关于 @laiyonghao 的这篇有点争议的博文<2012 不宜进入的三个技术点>A ...

  4. Spark:大数据的电花火石!

    什么是Spark?可能你很多年前就使用过Spark,反正当年我四六级单词都是用的星火系列,没错,星火系列的洋名就是Spark. 当然这里说的Spark指的是Apache Spark,Apache Sp ...

  5. zz 【见闻八卦】《金融时报》年度商业书单:互联网题材占一半

    [见闻八卦]<金融时报>年度商业书单:互联网题材占一半 文 / 见闻学堂 2014年12月18日 09:47:38 0   中国最好的金融求职培训:见闻学堂(微信号:top-elites) ...

  6. DDoS攻防战 (一) : 概述

    岁寒 然后知松柏之后凋也 ——论语·子罕 (此图摘自<Web脚本攻击与防御技术核心剖析>一书,作者:郝永清先生)    DDoS,即 Distributed Denial of Servi ...

  7. 转发 DDoS攻防战 (一) : 概述

     岁寒 然后知松柏之后凋也   岁寒 然后知松柏之后凋也 ——论语·子罕 (此图摘自<Web脚本攻击与防御技术核心剖析>一书,作者:郝永清先生)    DDoS,即 Distributed ...

  8. NO.009-2018.02.14《临江仙·送钱穆父》宋代:苏轼

    临江仙·送钱穆父_古诗文网 临江仙·送钱穆父 宋代:苏轼 一别都门三改火,天涯踏尽红尘.依然一笑作春温.无波真古井,有节是秋筠.自从我们在京城分别一晃又三年,远涉天涯你奔走辗转在人间.相逢一笑时依然像 ...

  9. NO.007-2018.02.12《白头吟》两汉:卓文君

    白头吟_古诗文网_解析_鉴赏_赏析 白头吟 两汉:卓文君 白头吟:乐府<楚调曲>调名.据<西京杂记>卷三载,蜀地巨商卓王孙的女儿卓文君,聪明美丽,有文采,通音乐.孀居在家时,与 ...

随机推荐

  1. C# ashx与html的联合使用

    本文将介绍ashx和html的联合使用方法,尽管目前流行mvc,但handler一般处理程序还是ASP.NET的基础知识,结合html页面,做出来的网页绝对比WebForm的简洁和效率高. 首先,概要 ...

  2. 9.23 noip模拟试题

      Problem 1 抓牛(catchcow.cpp/c/pas) [题目描述] 农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来. 他们都站在数轴上.约翰在N(O≤N ...

  3. IntelliJ IDEA 14

    新接触IntelliJ IDEA 14,使用起来还不是很称手,每天在使用中学习吧. 每学到一个新技能就来更新一下. (2015.11.17) " Ctrl + / " 代码批量注释 ...

  4. iOS sizeWithFont 过期 is deprecated

    原文: http://www.cnblogs.com/A--G/p/4819189.html iOS 2.0之后 sizeWithFont就被弃用了: //计算textview 高度 - (float ...

  5. [转]mysql自动定时备份数据库的最佳方法-支持windows系统

    网上有很多关于window下Mysql自动备份的方法,可是真的能用的也没有几个,有些说的还非常的复杂,难以操作. 我们都知道mssql本身就自带了计划任务可以用来自动备份,可是mysql咱们要怎么样自 ...

  6. 子树大小平衡树(Size Balanced Tree,SBT)操作模板及杂谈

    基础知识(包括但不限于:二叉查找树是啥,SBT又是啥反正又不能吃,平衡树怎么旋转,等等)在这里就不(lan)予(de)赘(duo)述(xie)了. 先贴代码(数组模拟): int seed; int ...

  7. c++primerplus(第六版)编程题——第3章(数据类型)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. 工程命名和文件命名可以命名成易识 ...

  8. Linux系统、版本、CPU、内存查看、硬盘空间

    查看系统版本:lsb_release -a [root@localhost /]# lsb_release -a LSB Version:    :core-4.0-amd64:core-4.0-no ...

  9. Valid Phone Numbers

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  10. shell 脚本中 命令

    终端工具tput和stty是两款终端处理工具tput cols,lines,longname,cpu 100 100 输入密码时,不能让输入的内容显示出来.用stty #!/bin/bash #Fil ...