IOI 2009:Mecho

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

Prev

Submit Status Statistics Discuss

Next

Type:

None

None

Graph Theory

2-SAT

Articulation/Bridge/Biconnected Component

Cycles/Topological Sorting/Strongly Connected Component

Shortest Path

Bellman Ford

Dijkstra/Floyd Warshall

Euler Trail/Circuit

Heavy-Light Decomposition

Minimum Spanning Tree

Stable Marriage Problem

Trees

Directed Minimum Spanning Tree

Flow/Matching

Graph Matching

Bipartite Matching

Hopcroft–Karp Bipartite Matching

Weighted Bipartite Matching/Hungarian Algorithm

Flow

Max Flow/Min Cut

Min Cost Max Flow

DFS-like

Backtracking with Pruning/Branch and Bound

Basic Recursion

IDA* Search

Parsing/Grammar

Breadth First Search/Depth First Search

Advanced Search Techniques

Binary Search/Bisection

Ternary Search

Geometry

Basic Geometry

Computational Geometry

Convex Hull

Pick‘s Theorem

Game Theory

Green Hackenbush/Colon Principle/Fusion Principle

Nim

Sprague-Grundy Number

Matrix

Gaussian Elimination

Matrix Exponentiation

Data Structures

Basic Data Structures

Binary Indexed Tree

Binary Search Tree

Hashing

Orthogonal Range Search

Range Minimum Query/Lowest Common Ancestor

Segment Tree/Interval Tree

Trie Tree

Sorting

Disjoint Set

String

Aho Corasick

Knuth-Morris-Pratt

Suffix Array/Suffix Tree

Math

Basic Math

Big Integer Arithmetic

Number Theory

Chinese Remainder Theorem

Extended Euclid

Inclusion/Exclusion

Modular Arithmetic

Combinatorics

Group Theory/Burnside‘s lemma

Counting

Probability/Expected Value

Others

Tricky

Hardest

Unusual

Brute Force

Implementation

Constructive Algorithms

Two Pointer

Bitmask

Beginner

Discrete Logarithm/Shank‘s Baby-step Giant-step Algorithm

Greedy

Divide and Conquer

Dynamic Programming

Tag it!

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 3TTTTTTTTGGGGGTTGGGGGTMGGGGGDTGGGGGTTGGGGGTTHHHHHT

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

IOI 2009

  这题很简单,但容易打错。

  题解什么的都略过了吧~~~

  吾不言。

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <queue>
  5 using namespace std;
  6 const int INF=233333333;
  7 char map[810][810];
  8 int dis[810][810];
  9 int Time[810][810];
 10 int n,k;
 11 struct Data{
 12     int x,y;
 13 }S;
 14 void P()
 15 {
 16     queue<Data>q;
 17     for(int i=1;i<=n;i++)
 18         for(int j=1;j<=n;j++)
 19             if(map[i][j]==‘H‘)
 20                 q.push((Data){i,j});
 21             else if(map[i][j]!=‘T‘)
 22                 dis[i][j]=INF;
 23
 24     while(!q.empty())
 25     {
 26         Data node=q.front();q.pop();
 27         if((map[node.x+1][node.y]==‘G‘||map[node.x+1][node.y]==‘M‘)
 28                     &&dis[node.x+1][node.y]>dis[node.x][node.y]+1)
 29             dis[node.x+1][node.y]=dis[node.x][node.y]+1,q.push((Data){node.x+1,node.y});
 30         if((map[node.x-1][node.y]==‘G‘||map[node.x-1][node.y]==‘M‘)
 31                     &&dis[node.x-1][node.y]>dis[node.x][node.y]+1)
 32             dis[node.x-1][node.y]=dis[node.x][node.y]+1,q.push((Data){node.x-1,node.y});
 33         if((map[node.x][node.y+1]==‘G‘||map[node.x][node.y+1]==‘M‘)
 34                     &&dis[node.x][node.y+1]>dis[node.x][node.y]+1)
 35             dis[node.x][node.y+1]=dis[node.x][node.y]+1,q.push((Data){node.x,node.y+1});
 36         if((map[node.x][node.y-1]==‘G‘||map[node.x][node.y-1]==‘M‘)
 37                     &&dis[node.x][node.y-1]>dis[node.x][node.y]+1)
 38             dis[node.x][node.y-1]=dis[node.x][node.y]+1,q.push((Data){node.x,node.y-1});
 39     }
 40     return;
 41 }
 42
 43 bool DFS(int t)
 44 {
 45     if(t>=dis[S.x][S.y])return false;
 46     queue<Data>q;
 47     q.push((Data){S.x,S.y});
 48     for(int i=1;i<=n;i++)
 49         for(int j=1;j<=n;j++)
 50             Time[i][j]=INF;
 51     Time[S.x][S.y]=t*k+1;
 52
 53     while(!q.empty())
 54     {
 55         Data node=q.front();q.pop();
 56         int nt=Time[node.x][node.y];
 57         if(dis[node.x+1][node.y]>nt/k&&Time[node.x+1][node.y]>nt+1){
 58             Time[node.x+1][node.y]=nt+1;q.push((Data){node.x+1,node.y});
 59             if(map[node.x+1][node.y]==‘D‘)
 60                 return true;
 61         }
 62
 63
 64         if(dis[node.x-1][node.y]>(nt)/k&&Time[node.x-1][node.y]>nt+1){
 65             Time[node.x-1][node.y]=nt+1,q.push((Data){node.x-1,node.y});
 66             if(map[node.x-1][node.y]==‘D‘)
 67                 return true;
 68         }
 69
 70
 71         if(dis[node.x][node.y+1]>(nt)/k&&Time[node.x][node.y+1]>nt+1){
 72             Time[node.x][node.y+1]=nt+1,q.push((Data){node.x,node.y+1});
 73             if(map[node.x][node.y+1]==‘D‘)
 74                 return true;
 75         }
 76
 77
 78         if(dis[node.x][node.y-1]>(nt)/k&&Time[node.x][node.y-1]>nt+1){
 79             Time[node.x][node.y-1]=nt+1,q.push((Data){node.x,node.y-1});
 80             if(map[node.x][node.y-1]==‘D‘)
 81                 return true;
 82         }
 83     }
 84     return false;
 85 }
 86 int main()
 87 {
 88     scanf("%d%d",&n,&k);
 89     for(int i=1;i<=n;i++)
 90         scanf("%s",map[i]+1);
 91
 92     for(int i=1;i<=n;i++)
 93         for(int j=1;j<=n;j++)
 94             if(map[i][j]==‘M‘){
 95                 S.x=i;S.y=j;
 96             }
 97     P();
 98     int lo=0,hi=10000;
 99     while(lo<=hi)
100     {
101         int mid=(lo+hi)>>1;
102         if(DFS(mid))lo=mid+1;
103         else hi=mid-1;
104     }
105     printf("%d\n",hi);
106     return 0;
107 }
时间: 2024-10-13 16:15:04

IOI 2009:Mecho的相关文章

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

Why C++ ? 王者归来(转载) 因为又有人邀请我去Quora的C2C网站去回答问题去了,这回是 关于 @laiyonghao 的这篇有点争议的博文<2012 不宜进入的三个技术点>ActionScript,Thread 和 C++,C++争议的争议最大.(要我说,.NET比C++更需要慎重进入,呵).我就在这里回复一下这个问题吧. 正好我一个月前看到一个视频,这个演讲视频还比较著名,这个演讲者是Exceptional C++ 和 C++ Coding Standards 的作者,还是IS

Spark:大数据的电花火石!

什么是Spark?可能你很多年前就使用过Spark,反正当年我四六级单词都是用的星火系列,没错,星火系列的洋名就是Spark. 当然这里说的Spark指的是Apache Spark,Apache Spark?is a fast and general engine for large-scale data processing: 一种快速通用可扩展的数据分析引擎.如果想要搞清楚Spark是什么,那么我们需要知道它解决了什么问题,还有是怎么解决这些问题的. Spark解决了什么问题? 在这里不得不

DDoS攻防战 (一) : 概述

岁寒 然后知松柏之后凋也 --论语·子罕 (此图摘自<Web脚本攻击与防御技术核心剖析>一书,作者:郝永清先生)    DDoS,即 Distributed Denial of Service ,可译为分散式阻断服务攻击. 上图与DDoS的字面已经清楚的表述出了此类攻击的原理,勿需多言.这类攻击泛滥存在的主要原因之一是网络服务的开放性,这一特点导致了DDoS攻击无法根本杜绝,目前主要应对策略是积极防御与消极防御. 典型DDoS的攻击方式:     ·死亡之Ping icmp封装于IP报文之中,

[连载]Java程序设计(01)---任务驱动方式:英制单位转换成公制单位

任务:你所在的公司是一家美国的服装设计和制造公司,现在这家公司打算进入欧洲市场,于是需要一个将英制单位(英寸)换算为公制单位(厘米)的程序.已知1英寸=2.54厘米,该程序输入以英寸为单位的长度,显示该长度对应的厘米数.例如:输入英寸:2.5,输出:2.5英寸=6.35厘米. package com.lovo; import java.util.Scanner; public class MyConverter { public static void main(String[] args) {

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

[见闻八卦]<金融时报>年度商业书单:互联网题材占一半 文 / 见闻学堂 2014年12月18日 09:47:38 0 中国最好的金融求职培训:见闻学堂(微信号:top-elites) 年末已至,假期还会远吗?是该为假期囤好书的时候啦.如果你正在为选择哪本书而焦虑,那么由<金融时报>为你把关的年度书单绝对值得收藏! 今天重点介绍<金融时报>所公布的2014年度最佳商业图书,托马斯•皮凯蒂(Thomas Piketty)所著的<21世纪资本论>荣获最佳,另外5

NO.003-2018.02.10《江城子&#183;乙卯正月二十日夜记梦》宋代:苏轼

江城子·乙卯正月二十日夜记梦_古诗文网 江城子·乙卯正月二十日夜记梦 乙卯:公元1075年,即北宋熙宁八年. 宋代:苏轼 十年生死两茫茫,不思量,自难忘.千里孤坟,无处话凄凉.纵使相逢应不识,尘满面,鬓如霜.两人一生一死,隔绝十年,相互思念却很茫然,无法相见.不想让自己去思念,自己却难以忘怀.妻子的孤坟远在千里,没有地方跟她诉说心中的凄凉悲伤.即使相逢也应该不会认识,因为我四处奔波,灰尘满面,鬓发如霜.十年:指结发妻子王弗去世已十年.思量:想念."量"按格律应念平声liáng千里:王弗

Atitit js es5 es6新特性 attilax总结

1.1. JavaScript发展时间轴:1 1.2. 以下是ES6排名前十的最佳特性列表(排名不分先后):1 1.3. Es6 支持情况 基本chrome ff 360se8全面支持了2 2. ECMAScript 2015(ES6)的十大特征 – WEB前端开发 - 专注前端开发,关注用户体验.html2 1.1. JavaScript发展时间轴: 1.1995:JavaScript诞生,它的初始名叫LiveScript. 2.1997:ECMAScript标准确立. 3.1999:ES3出

神经网络部分历史

* 1958:Perceptron诞生 * 1960:梯度下降 的使用 # 1962:视觉皮层中的simple cells和complex cells被发现. $ 1971:第一个CPU Intel 4004诞生.频率 108KHz-740KHz. $ 1985:CPU 386诞生.频率 12MHz-33MHz. * 1986:反向传播 开始广泛使用 * 1989:卷积神经网络 $ 1993:CPU性能参考.Pentium.频率 60MHz-300MHz. * 1997:Long-Short T

前端开发者不得不知的ES6十大特性

前端开发者不得不知的ES6十大特性 转载 作者:AlloyTeam 链接:http://www.alloyteam.com/2016/03/es6-front-end-developers-will-have-to-know-the-top-ten-properties/ ES6(ECMAScript2015)的出现,无疑给前端开发人员带来了新的惊喜,它包含了一些很棒的新特性,可以更加方便的实现很多复杂的操作,提高开发人员的效率. 本文主要针对ES6做一个简要介绍. 主要译自:  http://