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

一开始想用dfs,超过三堆就输出-1,两堆就输出较大一堆的最小值,但是发现一堆的时候没法算

比如这种的时候答案是1,dfs做不出来

.##.

.##.

所以用bfs,发现其实bfs也不难哈哈哈,因为只有10*10,所以可以暴力每两个点,O(n^2)复杂度,不会T,记录可以烧光的两个点的时间,输出最小的就好了

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
//#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
//#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
int n,m,sum;
int dir[4][2]={0,-1,0,1,-1,0,1,0};
int ans=mod,visit[15][15];
struct node
{
int x,y;
int state;
node(int a,int b,int c)
{
x=a;y=b;state=c;
}
node(){
}
};
char map[15][15],a[15][15];
queue<node>v;
bool bfs(node a,node b)
{
if(map[a.x][a.y]=='#')
v.push(a);
if(map[b.x][b.y]=='#')
v.push(b);
if(v.empty()) return false;
node t,tt;
while(!v.empty())
{
t=v.front();
v.pop();
rep(i,0,4)
{
tt.x=t.x+dir[i][0];
tt.y=t.y+dir[i][1];
tt.state=t.state+1;
if(map[tt.x][tt.y]=='#'&&visit[tt.x][tt.y]==0)
{
visit[tt.x][tt.y]=1;
v.push(tt);
sum=max(sum,tt.state);
}
}
}
rep(i,1,n+1)
rep(j,1,m+1)
if(map[i][j]=='#'&&visit[i][j]==0)
return false;
return true;
}
int main()
{
int re;
cin>>re;
rep(m9,1,re+1)
{
ans=mod;
mm(map,'.');
cin>>n>>m;
rep(i,1,n+1)
{
sf("%s",&map[i][1]);
map[i][m+1]='.';
}
int count=0;
mm(a,'.');
rep(i,1,n+1)
rep(j,1,m+1)
{
if(map[i][j]=='#')
count++;
}
if(count<=2)//少于等于两堆一次就烧光了
{
pf("Case %d: 0\n",m9);
continue;
}
rep(i,1,n+1)
{
rep(j,1,m+1)
{
rep(k,1,n+1)
{
rep(l,1,m+1)
{
mm(visit,0);
sum=0;
visit[i][j]=1;visit[k][l]=1;
if(bfs(node(i,j,0),node(k,l,0)))
if(sum)//这个判断似乎多余了,因为bfs可以烧光的话,sum不会等于0,应该可以删了,懒得试,
{
ans=min(ans,sum);
}
}
}
}
}
if(ans==mod)//等于mod的时候说明没有办法烧光
pf("Case %d: -1\n",m9);
else
pf("Case %d: %d\n",m9,ans);
}
return 0;
}

L - Fire Game的更多相关文章

  1. Quartz.net配置文件实例及cron表达式详解

    从XML文件创建作业 最新版本的quartz.net支持直接从xml文件创建作业,使用起来很方便.配置文件的格式可以参考下面的例子 <?xml version="1.0" e ...

  2. quartz集群分布式(并发)部署解决方案-Spring

    项目中使用分布式并发部署定时任务,多台跨JVM,按照常理逻辑每个JVM的定时任务会各自运行,这样就会存在问题,多台分布式JVM机器的应用服务同时干活,一个是加重服务负担,另外一个是存在严重的逻辑问题, ...

  3. 注解式开发spring定时器

    1:spring 配置文件中增加这句    <task:annotation-driven/>  2:确保扫描程序能够扫描后  下面第3步骤的java类    <context:co ...

  4. 任务调度框架-Quartz.Net

    使用Quartz.Net依赖于以下3个组件:Common.Logging.dll.Common.Logging.Core.dll.Quartz.dll 简单封装 using Quartz; using ...

  5. Quartz集群

    为什么选择Quartz: 1)资历够老,创立于1998年,比struts1还早,但是一直在更新(27 April 2012: Quartz 2.1.5 Released),文档齐全. 2)完全由Jav ...

  6. Quartz:Cron Expressions

    原文地址:http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/crontrigger.html 注意: 位也可能是7位, ...

  7. Quartz 2.2.x CronTrigger Tutorial

    http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/crontrigger.html Introduction c ...

  8. 项目中使用Quartz集群分享--转载

    项目中使用Quartz集群分享--转载 在公司分享了Quartz,发布出来,希望大家讨论补充. CRM使用Quartz集群分享  一:CRM对定时任务的依赖与问题  二:什么是quartz,如何使用, ...

  9. Quartz Scheduler(2.2.1) - Usage of CronTriggers

    Cron is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerfu ...

随机推荐

  1. 一步一步开发sniffer(Winpcap+MFC)(五)莫道无人能识君,其实我懂你的心——解析数据包(转)

    前文已经讲过,解析数据包主要通过analyze_frame()这个函数实现的,实际上并非这个函数完成了所有的功能,其实从名字就可以看出,它只是完成了对“帧”的解析,也就是链路层数据的解析,还有anal ...

  2. SpringCloud实战2-Ribbon客户端负载均衡

    https://www.cnblogs.com/huangjuncong/p/9022055.html

  3. 【Zookeeper】源码分析之服务器(二)之ZooKeeperServer

    一.前言 前面阐述了服务器的总体框架,下面来分析服务器的所有父类ZooKeeperServer. 二.ZooKeeperServer源码分析 2.1 类的继承关系 public class ZooKe ...

  4. Java 迭代器综述

    一.摘要 迭代器模式是与集合共生共死的.一般来说.我们仅仅要实现一个容器,就须要同一时候提供这个容器的迭代器.使用迭代器的优点是:封装容器的内部实现细节,对于不同的集合,能够提供统一的遍历方式,简化c ...

  5. 【CLR】解析CLR的托管堆和垃圾回收

    目录结构: contents structure [+] 为什么使用托管堆 从托管堆中分配资源 托管堆中的垃圾回收 垃圾回收算法 代 垃圾回收模式 垃圾回收触发条件 强制垃圾回收 监视内存 对包装了本 ...

  6. php write_ini_file

    php读ini文件有很方便的pares_ini_file,但是写回去却没有,这里写一个: function write_ini_file($assoc_arr, $path, $has_section ...

  7. 12C -- ORA-12850: 无法在所有指定实例上分配从属进程: 需要 2, 已分配 1

    使用客户端连接到oracle 12.2.0.1 rac数据库,报以下错误信息: ORA-12850: 无法在所有指定实例上分配从属进程: 需要 2, 已分配 1 因为没有mos账号,只好谷歌一下了.找 ...

  8. CentOS SVN服务器管理多项目

    一 需求 一般来说,公司有多个项目,在搭建好SVN服务器之后,就需要使用SVN来实现不在一个项目中的开发人员不能访问其它项目中的代码. 假设: 有3个项目:project1.project2.proj ...

  9. 转: 如何使用jstack分析线程状态

    这个讲的好系列:  如何使用jstack分析线程状态 转:http://www.jianshu.com/p/6690f7e92f27 背景 记得前段时间,同事说他们测试环境的服务器cpu使用率一直处于 ...

  10. Jenkins Post Build网址

    Hudson Post build taskhttps://plugins.jenkins.io/postbuild-taskThis plugin allows the user to execut ...