Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

You can assume that the grass in the board would never burn out and the empty grid would never get fire.

Note that the two grids they choose can be the same.

Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers N and M
indicate the size of the board. Then goes N line, each line with M
character shows the board. “#” Indicates the grass. You can assume that
there is at least one grid which is consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

Output

For each case, output the case number first, if they can play the
MORE special (hentai) game (fire all the grass), output the minimal time
they need to wait after they set fire, otherwise just output -1. See
the sample input and output for more details.

Sample Input

4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#

Sample Output

Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2 题意,从两个地方点火,看看烧完最短的时间是多少,火不能烧'.',只能烧'#',也就是遇到了句点,火焰停止蔓延;
一开始想看看有没有啥规律,想判断如果连通分支超过两个就不行,然后分开讨论一个连通分支与两个连通分支的情况,可是规律不通用,于是选择了排着遍历,mint给一个很大的值,每次选两个地方进行点火,然后都入队,bfs,如果能把草全烧完,更新最小时间,最后如果最小时间还是原来很大的值,输出-1.
但是忘记了还有只有一块草的情况,那就另外加了一下就过了。 代码:
#include <iostream>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std; int dir[][]={,,,,,-,-,};
int T,n,m,x,y,t,vis[][],c;
char mp[][];
class que
{
public:
int x,y,time;
}temp;
int xx[],yy[],xy,mint;
int main()
{
cin>>T;
for(int l=;l<=T;l++)
{
mint=;//updated
cin>>n>>m;
queue<que>q;
xy=;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='#')
xx[xy]=i,yy[xy]=j,xy++;
}
for(int i=;i<xy;i++)
{
for(int j=i+;j<xy;j++)
{
memset(vis,,sizeof(vis));//initialized
temp.x=xx[i],temp.y=yy[i],temp.time=;
q.push(temp);
vis[temp.x][temp.y]=;
temp.x=xx[j],temp.y=yy[j],temp.time=;
q.push(temp);
vis[temp.x][temp.y]=;
c=;//updated
t=;//updated
while(!q.empty())
{
if(t<q.front().time)t=q.front().time;
for(int k=;k<;k++)
{
x=q.front().x+dir[k][];
y=q.front().y+dir[k][];
if(x<||y<||x>=n||y>=m||mp[x][y]=='.'||vis[x][y])continue;
temp.x=x,temp.y=y,temp.time=q.front().time+;
q.push(temp);
vis[x][y]=;
c++;
}
q.pop();
}
if(c==xy){if(t<mint)mint=t;}//judged
} }
printf("Case %d: ",l);
if(mint<)cout<<mint<<endl;
else if(xy==)cout<<<<endl;//special
else cout<<-<<endl; }
}

Fire Game 双向bfs的更多相关文章

  1. UVA - 11624 Fire! 双向BFS追击问题

    Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...

  2. CSUOJ2031-Barareh on Fire(双向BFS)

    Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...

  3. ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

    FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  4. POJ1915Knight Moves(单向BFS + 双向BFS)

    题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...

  5. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  6. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...

  7. [转] 搜索之双向BFS

    转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...

  8. 双向BFS

    转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2)  快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...

  9. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

随机推荐

  1. springboot集成mybatis及mybatis generator工具使用

    原文链接 前言mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernat ...

  2. Freemarker 简介

    1.动态网页和静态网页差异 在进入主题之前我先介绍一下什么是动态网页,动态网页是指跟静态网页相对应的一种网页编程技术.静态网页,随着HTML代码的生成,页面的内容和显示效果就不会再发生变化(除非你修改 ...

  3. Go开发环境安装配置

    访问下载地址:https://golang.org/dl/ 32位系统下载go1.8.1.linux-386.tar.gz,64位系统下载go1.8.1.linux-amd64.tar.gz, 假定你 ...

  4. 【异常】Caused by: java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around

    原因:缺少aspect,AOP的maven坐标 导入maven坐标: <dependency> <groupId>org.springframework</groupId ...

  5. 牛客练习赛23-A/B/C/D/F

    https://www.nowcoder.com/acm/contest/156#question 链接:https://www.nowcoder.com/acm/contest/156/A来源:牛客 ...

  6. intel python加速效果初探

    python3安装intel的加速库: conda config --add channels intel conda create --name intelpy intelpython3_full ...

  7. shiro学习笔记-Subject#login(token)实现过程

    本博文所有的代码均为shiro官网(http://shiro.apache.org/)中shiro 1.3.2版本中的源码. 追踪Subject的login(AuthenticationToken t ...

  8. UVSLive 6324 求射箭覆盖的期望

    DES:给出n条线段.询问每次射中线段条数的期望. 非常简单.就是每条线段的两端与原点相连的直线之间的夹角和.如果夹角大于pi.就是2pi减去这个角.最后除以总值2pi就是所求的期望. atan2(y ...

  9. Android开发——1轻松战胜开发环境

    写在前头的话:鄙人乃2016年本科毕业的程序yuan一枚,大学阶段从未学过安卓,java也是一知半解,回想这一年半的开发生涯真的是相当悲壮.你要是问我喜欢开发吗,当然确定一定以及肯定地告诉你不喜欢啊! ...

  10. robot framework学习笔记之六—自动变量

    自动变量被RF框架创建和修改,在测试执行过程中值可能会变化;另外,某些自动变量在执行过程中并非总是可用.修改自动变量,并不能对变量的初始值产生影响.但可以用某些内建关键字来修改某些自动变量的值. 变量 ...