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. 修改unity变量名但不丢失序列化值

    using UnityEngine; using UnityEngine.Serialization; public class LgsTest : MonoBehaviour { [Formerly ...

  2. LeetCode--119--杨辉三角II

    问题描述: 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: 你可以优化你 ...

  3. android--------阿里 AndFix 热修复

    AndFix,全称是Android hot-fix.是阿里开源的一个热补丁框架,允许APP在不重新发布版本的情况下修复线上的bug. 支持Android 2.3 到 6.0,并且支持arm 与 X86 ...

  4. Spring Boot 文档

    本节对 Spring Boot 的参考文档做了一个简单概述.本章节对全文的参考手册进行内容上的一些索引. 你可以参考本节,从头到尾依次阅读该文档,也可以跳过不感兴趣的内容. Spring Boot 参 ...

  5. Oracle11g温习-第八章:归档日志

    2013年4月27日 星期六 10:36 1.归档和非归档的区别 1)  归档会在日志切换时,备份历史日志,用于OLTP(在线事务处理系统),可以进行冷备份和热备份,可以实现数据库完全恢复.不完全恢复 ...

  6. Leetcode 77

    //这似乎是排列组合的标准写法了已经class Solution { public: vector<vector<int>> combine(int n, int k) { v ...

  7. 使用POI导入EXCEL报java.lang.IncompatibleClassChangeError

    使用POI导入xls格式的excel报java.lang.IncompatibleClassChangeError异常,而导入xlsx正常. oracle.apps.fnd.framework.OAE ...

  8. ps -ef |grep xxx 输出的具体含义

    ps:将某个进程显示出来 -A 显示所有程序. -e 此参数的效果和指定"A"参数相同. -f 显示UID,PPIP,C与STIME栏位. grep命令是查找 中间的|是管道命令 ...

  9. Activiti工作流笔记(3)

    Activiti工作流的流程部署和删除流程部署 流程部署代码: /** * 部署流程 */ public class ActivitiTest { RepositoryService reposito ...

  10. OC Foundation框架—字符串操作方法及习题

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...