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. 百度echart如何动态生成图表

    百度echart如何动态生成图表 一.总结 一句话总结: clear hideloading setOption 主要是下面三行代码: myChart.clear(); //清空画布myChart.h ...

  2. CRISPR基因编辑

    2007年,一家酸奶公司发现一种细菌拥有抵御病毒的特殊防御机制.2012年,细菌的这种机制就被科学家阐述清楚,2013年这一领域快速增长.它被视为分子生物学的一个奇迹,事实上,它并非仅对生物学家产生革 ...

  3. 实训10a--用数据值填充下拉列表

    1.新建mvc4项目,选择基本模板. (1)点击“开始->所有程序->Microsoft Visual Studio 2012->Visual Studio 2012”菜单,打开Vi ...

  4. Confluence 6 导入 Active Directory 服务器证书 - Mac OS X

    为了让你的应用服务器能够信任你的目录服务器.你目录服务器上导出的证书需要导入到你应用服务器的 Java 运行环境中.JDK 存储了信任的证书,这个存储信任证书的文件称为一个 keystore.默认的 ...

  5. Nanami's Digital Board CodeForces - 434B (棋盘dp)

    大意: 给定01矩阵, m个操作, 操作1翻转一个点, 操作2求边界包含给定点的最大全1子矩阵 暴力枚举矩形高度, 双指针统计答案 #include <iostream> #include ...

  6. C++中的赋值运算符重载函数(operator=)

    MyStr& operator =(const MyStr& str)//赋值运算符 { cout << "operator =" << e ...

  7. POJ-2415 Hike on a Graph (BFS)

    Description "Hike on a Graph" is a game that is played on a board on which an undirected g ...

  8. Oracle11g温习-第四章:手工建库

     1.create database plan 1.库类型:   OLTP :  在线事务处理系统   OLAP : 在线应用处理系统   DSS :    数据决策系统 2.数据库名字及字符集 3. ...

  9. 65. Valid Number *HARD*

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  10. OC 继承

    一.基本概念 程序的世界和人类的“对象”世界在思想上是没有设么区别的,富二代继承了父母,自然就拥有了父母拥有的所有资源,子类继承了父类同样就拥有了父类所有的方法和属性(成员变量). 在这里动物是猫类和 ...