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. Windows开启Telnet

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/4301513.html ...

  2. C++链表与键值对

    <算法>一书中,在算法3.1中提到了Map的实现,这里根据书上的思想,用单向链表简单写了写. #ifndef SEQUENTIAL_H #define SEQUENTIAL_H templ ...

  3. retain two decimal digits.

    package kju.o; import static kju.print.Printer.*; import java.text.*; class MathDemo { public static ...

  4. c# 为什么要用 get set 属性

    1 可以对赋值 做验证 ,范伟限制,额外的限制 2 可以设置 只读 只写 3 可以做线程同步 4 可以将属性设置在interface接口中 5 可以使用虚属性 或 抽象属性 可以填补 没有 虚字段 抽 ...

  5. 执行hadoop fs -ls时出现错误RuntimeException: core-site.xml not found

    由于暴力关机,Hadoop fs -ls 出现了下图问题: 问题出现的原因是下面红框框里面的东西,我当时以为从另一个节点下载一个conf.cloudera.yarn文件就能解决问题,发现不行啊,于是删 ...

  6. get方式编码问题解决方案 转载

    我们的内容使用GET方式发送,就会在URL后面带上内容,在游览器发来的请求经过了游览器的URI编码,发送到服务器这边,如果是struts2会经过拦截器进行URI解码,并且使用"iso8859 ...

  7. Linux技巧总结(个人经验版)

    1:善用桌面:1.图形界面的编辑,2.终端只要开机就在第2桌面,3.浏览器在第3桌面,4.娱乐在第4桌面. 2:cd命令中,输入中文目录很不方便,用 ln -s 桌面 desktop 创建软链接,不必 ...

  8. sql查询一个班级中总共有多少人以及男女分别多少人

    --创建视图 create view StuClassView as SELECT s.ID ,s.StuName ,s.StuAge ,s.StuAddress ,s.StuTel ,s.Class ...

  9. 【转载】详细解读C#中的 .NET 弱事件模式

    你可能知道,事件处理是内存泄漏的一个常见来源,它由不再使用的对象存留产生,你也许认为它们应该已经被回收了,但不是,并有充分的理由. 在这个短文中(期望如此),我会在 .Net 框架的上下文事件处理中展 ...

  10. discuz二次开发技巧

    discuz二次开发技巧 二次开发大多时候知识设置和处理,如果能够获知模板文件获得的变量数组将大大提高我们的开发效率 获取页面已经定义的变量 <--{eval printf_r(get_defi ...