Mecho

Problem Page (Codeforces IOI Archive)

Description

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 N×NN×NN×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 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 SSS 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 SSS 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.

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.

Format

Input

Your program must read from file input the following data:

  • The first line contains the integers NNN and SSS, separated by a space.
  • The next NNN lines represent the map of the forest. Each of these lines contains NNN 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

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

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 −1−1−1 instead.

Samples

Input

7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT

Output

1

Explanation

In the first 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.

Input

7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT

Output

2

Explanation

In the second 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.

Constraints

  • 100100100 points for 1≤N≤800,1≤S≤10001≤N≤800,1≤S≤10001≤N≤800,1≤S≤1000.

解析

暴力二分答案+搜索题。单调性易于看出,重点在于搜索验证。很暴力,没什么好说的。

但是,有很多坑点,我碰到了,希望各位不要碰到:

  1. while(!Q.empty() && Q.front().s<wait)
    

    必须在没有取出队头元素前判断时间是否越界。不然,取出后,因为这个队列对后面有影响,会导致一些区域应该被蜜蜂占领的却没被标记。

  2. 严格遵循题目描述:小熊先走,蜜蜂后走。

  3. 小熊的家 D 蜜蜂不可入。

  4. memset(vis, 0, sizeof(vis));
    
    vis[start.x][start.y]=1;
    

    这两句话的次序问题。

这样,这题就解决了。看上去这题很吓人,实际上算法也不高端。但是细节很多,码量巨大。详见代码。

代码

// C
/*
Input
7 3
GGGGGGG
GTTGTTG
GTTGTTG
MTTGTTD
GTTGTTG
GGGGGGG
HHHHHHH
Output
2Input
5 1
DGMGG
GGGGG
GGGGG
GGGGG
GHGHG
Output
2
*/
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#define SIZE 805
#define all(x) x.begin(), x.end()
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;int n, mxstep;
char a[SIZE][SIZE];
bool vis[SIZE][SIZE];
bool bee[SIZE][SIZE];
struct node
{int x, y;int s;bool operator ==(node b){return x==b.x && y==b.y;}
};
vector<node> hive;
node start;
node endpo;int dx[]={-1, 1, 0, 0};
int dy[]={0, 0, -1, 1};bool bfs(int wait)
{queue<node> q;   // bearq.push(start);queue<node> Q;   // beememset(vis, 0, sizeof(vis));memset(bee, 0, sizeof(bee));vis[start.x][start.y]=1;for(node t:hive){bee[t.x][t.y]=1;Q.push(t);}// 吃蜜时间蜜蜂围攻 while(!Q.empty() && Q.front().s<wait){node w=Q.front(); Q.pop();int i=w.x, j=w.y;for(int k=0; k<4; k++){int ni=i+dx[k];int nj=j+dy[k];// 不越界 if(ni<0 || ni>=n || nj<0 || nj>=n)continue;// 不走回头路 if(bee[ni][nj])continue;// 不入禁区 if(a[ni][nj]=='D' || a[ni][nj]=='T')continue;bee[ni][nj]=1;Q.push({ni, nj, w.s+1});}}// 原始位置已被攻占 if(bee[start.x][start.y])return 0;int steps=1;   // 行走的次数 while(!q.empty()){// 小熊先走 while(!q.empty() && q.front().s<steps*mxstep){node w=q.front(); q.pop();int i=w.x, j=w.y;if(w==endpo) return 1;if(bee[i][j]) continue;for(int k=0; k<4; k++){int ni=i+dx[k];int nj=j+dy[k];// 不越界if(ni<0 || ni>=n || nj<0 || nj>=n)continue;// 不被蜜蜂蛰 if(bee[ni][nj])continue;// 不走回头路 if(vis[ni][nj])continue;// 不入禁区 if(a[ni][nj]=='T')continue;vis[ni][nj]=1;q.push({ni, nj, w.s+1});}}// 蜜蜂后走while(!Q.empty() && Q.front().s<wait+steps){node w=Q.front(); Q.pop();int i=w.x, j=w.y;for(int k=0; k<4; k++){int ni=i+dx[k];int nj=j+dy[k];// 不越界if(ni<0 || ni>=n || nj<0 || nj>=n)continue;// 不走回头路 if(bee[ni][nj])continue;// 不入禁区 if(a[ni][nj]=='D' || a[ni][nj]=='T')continue;bee[ni][nj]=1;Q.push({ni, nj, w.s+1});}}steps++;}return 0;
}signed main()
{cin>>n>>mxstep;int si, sj;for(int i=0; i<n; i++)for(int j=0; j<n; j++){cin>>a[i][j];if(a[i][j]=='H')hive.push_back({i, j, 0});if(a[i][j]=='M')start={i, j, 0};if(a[i][j]=='D')endpo={i, j, 0};}int l=0, r=n*n;int ans=-1;while(l<=r){int m=(l+r)>>1;if(bfs(m)){ans=m;l=m+1;}elser=m-1;}cout<<ans;return 0;
}

IOI2009 Day2 B Mecho小熊相关推荐

  1. Day2 - Python基础2作业【文件操作--购物车程序(用户操作及商户操作)】

    1 # ----user.txt---- 2 3 {'已购商品': '', '消费记录': '', '余额': 0} 4 5 6 # ----commodity.txt---- 7 8 iPhone, ...

  2. 【JAVA零基础入门系列】Day2 Java集成开发环境IDEA

    [JAVA零基础入门系列](已完结)导航目录 Day1 开发环境搭建 Day2 Java集成开发环境IDEA Day3 Java基本数据类型 Day4 变量与常量 Day5 Java中的运算符 Day ...

  3. Alpha冲刺Day2

    冲刺Day2 一.站立式会议计划 分组讨论研究:较好的掌握MYSQL的使用,以及Android Studio图形化界面设计的学习同步进行. 完成设计数据库架构,进阶版. 登录.注册界面的设计. 能从同 ...

  4. ZJOI2019 Day2 游记

    emmm,一直没有更新不是因为退役了自闭什么的,只是单纯比较懒.写游记很累的. 这次余姚之旅中我似乎并没有怎样焦急和兴奋,回想起来,我甚至比一试时要平静得多. 是因为挫折让人有些长大了吗? 二试讲课时 ...

  5. 【从零开始学BPM,Day2】默认表单开发

    [课程主题] 主题:5天,一起从零开始学习BPM [课程形式] 1.为期5天的短任务学习 2.每天观看一个视频,视频学习时间自由安排. [第二天课程] Step 1 软件下载:H3 BPM10.0全开 ...

  6. 展示前一阵子做的小熊!

          前些天做的小熊,四肢能动的,就是样子好难看.因为剪布料时候没注意,在熊的头上部有两个圆点,一上一下斜着,做成后可难看,老公说像受了核污染的熊.这么难看也没法让他出来见人,上周在家给他做了改 ...

  7. .net 数字转汉字_[原创工具] 小熊汉字笔顺学习软件,查笔顺、学拼音、制作汉字英文数字字贴...

    点击右上角"设为星标"每日精彩内容,第一时间送达! 前言 今天带来的是原创软件.家里有上一二年级的小朋友有福了!家里有打印机的可以把设置好的字帖打印出来,小朋友即可临摹.赶紧下载使 ...

  8. 与小熊、新宝不同的北鼎,仍不是小家电的出路

    (图片来源于网络,侵删) 来源 | 螳螂观察 文 | 易不二 谁不向往美好的生活? 尤其是各种文章.视频里营造的那种"一室两人三餐四季"的美好生活,再配上万青那句"是谁来 ...

  9. 小熊电器、九阳、苏泊尔们的“颜价比”被外卖小哥“打回原形”

    (图片来源于网络) 文 | 易不二 来源 | 螳螂财经(ID:TanglangFin) "电饭锅蛋糕比较简单,我第一次尝试就做成功了,但是凉皮和油条难一点,一开始我做凉皮老是要断掉,油条炸出 ...

  10. MyBatis-Plus Day2 Wapper 核心功能 条件构造器 测试

    MyBatis-Plus Day2 核心功能 条件构造器 之前搭建的在上一篇博客中已经写好了. 链接:https://blog.csdn.net/weixin_45821811/article/det ...

最新文章

  1. 前端新手学习记录1 -使用vscode编辑html
  2. 现代软件工程 第十二章 【用户体验】练习与讨论
  3. B/S模式下如何使软件屏蔽系统热键
  4. 爬取58二手数据.py
  5. Centos系统搭建LAMP
  6. 计算机辐射对人体影响吗,电脑屏幕辐射对人体的危害怎么解决?
  7. 数据分析的流程是啥样?
  8. 计算机专业课程项目教学教学设计,高职旅游管理专业计算机课程项目化教学设计...
  9. ZOJ 1610 Count the Colors
  10. SQL中的全文检索(转帖)
  11. 【算法导论】0-1背包问题 与 部分背包
  12. tomcat配置及遇到的问题
  13. 微信小程序图片自适应大小(炒鸡详细)
  14. 申请百度云文字识别OCR
  15. Unity Activator反射
  16. python三维方式显示灰度图像(已验证)
  17. Java岗大厂面试百日冲刺 - 日积月累,每日三题【Day23】—— 算法1
  18. 研华PCI板卡开发(4)快速入门(4)轴操作
  19. 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第n次落地时,共经过多少米?第n次反弹多高?(以第十次为例)
  20. [W pthreadpool-cpp.cc:90] Warning: Leaking Caffe2 thread-pool after fork. (function pthreadpool)

热门文章

  1. html隐藏或显示不出来,win7隐藏文件显示不出来
  2. C语言指数函数应用pow n次方
  3. Cortex-A 架构
  4. react中使用构建缓存_如何使用React,GraphQL和Okta构建健康跟踪应用
  5. 射频识别技术在手机支付技术中的应用
  6. 游戏服务器 配置文件,游戏服务器应用配置文件
  7. IE6不兼问题之Internet Explorer 无法打开 internet站点
  8. 整理百度SEO指南2.0
  9. 季琦谈创业:三对矛盾和三个关口[转]
  10. 4键电子手表说明书_4键sport电子表使用说明书,按START键可循环选择12/24小时显示格式...