编程介的小学生 2017-08-22 02:51 采纳率: 0.2%
浏览 939
已采纳

Bee

Problem Description
QQ 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 QQ doesn’t want to leave too soon. Help QQ determine the latest possible moment when he can leave.
QQ’s forest is represented by a square grid of N by N 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 QQ’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). QQ is a clumsy bear, so every time he makes a step, it has to be to an adjacent cell. QQ can only walk on grass and cannot go through trees or hives, and he can make at most S steps per minute.
At the moment when the bee alarm is sounded, QQ 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:
1. If QQ 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 S steps through the forest as described above. QQ cannot take any of the honey with him, so once he has moved he cannot eat honey again.

  1. After QQ 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 QQ nor the bees can go outside the forest. Also, note that according to the rules above, QQ will always eat honey for an integer number of minutes.
The bees catch QQ if at any point in time QQ 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 QQ 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
1<=N<=800, the size (side length) of the map
1<=S<=1000, the maximum number of steps QQ can take in each minute

Input
The first line contains the integers N and S, separated by a space.
The next N lines represent the map of the forest. Each of these lines contains N 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 QQ and the honeypot, which is also a grassy cell
D denotes the location of QQ’s home, which QQ 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 G that connects QQ to his home, as well as a sequence of adjacent letters G that connects at least one hive to the honeypot (i.e., to QQ’s initial location). These sequences might be as short as length zero, in case QQ’s home or a hive is adjacent to QQ’s initial location.
Also, note that the bees cannot pass through or fly over QQ’s home. To them, it is just like a tree.

Output
output a single line containing a single integer: the
maximum possible number of minutes that QQ can continue eating honey at his initial location, while still being able to get home safely. If QQ cannot possibly reach his home before the bees catch him, the number your program writes to standard output must be -1 instead.

Sample Input
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT

7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT

Sample Output
1
2

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-09-03 14:30
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。