#BS0000420. Gấu Mecho (Mecho)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Gấu Mecho (Mecho)
Mecho
Source: IOI
Version: Phuoc Hung OJ Extended
Problem Statement
Mecho the bear is eating honey in a forest when the bees notice him and raise an alarm. He wants to keep eating for as long as possible, but must still reach his home without being caught.
The forest is an square grid. Cells are adjacent when they share an edge. Mecho may step into an adjacent grassy cell or his home, but cannot enter a tree or a hive. He can take at most steps in one minute.
Initially, Mecho is at the honey cell and the bees occupy all hive cells. In each minute the following events occur in order:
- If Mecho is still eating, he may stay and eat for the entire minute, or stop eating immediately and take up to steps. After leaving the honey cell he cannot resume eating.
- Once Mecho has finished eating or moving for that minute, bees simultaneously spread to every grassy cell adjacent to an occupied cell. Occupied cells remain occupied. Bees cannot enter trees or Mecho's home.
Neither Mecho nor the bees may leave the grid. Mecho is caught whenever he occupies a cell containing bees, including immediately after a spread. Find the largest integer number of minutes he can eat before departing and still reach home safely.
Input
The first line contains integers , the grid side length and the maximum number of steps per minute.
Each of the next lines contains exactly characters without spaces: T is a tree, G is grass, M is the grassy honey cell where Mecho starts, D is his home, and H is a hive.
Output
Print one integer, the maximum eating time in minutes. If even immediate departure cannot lead to a safe arrival, print -1.
Subtasks
- Subtask 1 (40 points): ; ; each of the rows contains exactly characters from
T,G,M,D,H; exactly oneM, exactly oneD, at least oneH; a path fromMtoDand a path from at least oneHtoMexist, each having onlyGcells as intermediate cells (possibly none). - Subtask 2 (60 points): ; ; each of the rows contains exactly characters from
T,G,M,D,H; exactly oneM, exactly oneD, at least oneH; a path fromMtoDand a path from at least oneHtoMexist, each having onlyGcells as intermediate cells (possibly none).
Examples
Input
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT
Output
1
Explanation
Using one-based coordinates, Mecho starts at (4,1) and home is (4,7). Eat one minute; take three rightward steps in minute 2 and three more in minute 3. Bees reach (4,6) at the end of minute 3, after Mecho enters home. Waiting two minutes leaves only three steps before bees permanently block (4,6), the sole grassy entrance to home. Print 1.
Input
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT
Output
2
Explanation
The hives are at (7,3) and (7,4), using one-based coordinates. Eat two minutes; go right, up, right in minute 3; right three times in minute 4; down and right in minute 5. Bees occupy (4,6) at the end of minute 5. Waiting three minutes leaves only (4,1), (3,2), or (2,2) as safe positions at the end of minute 4. Even the nearest is at least five steps from (4,6), but only three steps remain before it is occupied. Since (4,6) is the sole grassy entrance, print 2.
Input
2 1
MD
HT
Output
0
Explanation
Home is directly to the right, and a hive is directly below. Leaving immediately reaches home in one step. Eating for one minute lets bees occupy M. Therefore the maximum is 0.