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. vscode 中使用php-cs-fixer和PHP Formatter 插件规范化PHP代码

    什么是PHP-CS-Fixer?    它是php-fig组织定义的PHP代码规范,良好的代码规范可以提高代码可读性,团队沟通维护成本    使用它可以按照指定的规范格式化您的PHP代码,此工具不仅可 ...

  2. (转)Render Path

    Render Path定义Render Path,就是采取的光照流程. Render Path设置可以在Edit-> Project Settings->Player 里设定,见下图.也可 ...

  3. Spring中通过Annotation来实现AOP

    一.Spring配置文件 <!--通过@AspectJ注解的形式来使用Spring AOP,强制使用CGLIB代理--> <aop:aspectj-autoproxy proxy-t ...

  4. 〖Linux〗Ubuntu用户重命名、组重命名,机器重命名~

    有时候得到的一台机器名字并不是自己熟悉的,或许是你只是想希望修改一下用户名等等-- 步入正题,其实很简单的,重启机器之后不要进入桌面,按下Ctrl+Alt+F1,使用Root登录,执行以下命令: # ...

  5. 周期同步位置模式(CSP),轮廓位置模式(PPM),位置模式(PM)

    什么是运动控制? 运动控制就是通过机械传动装置对运动部件的位置.速度进行实时的控制管理,使运动部件按照预期的轨迹和规定的运动参数(如速度.加速度参数等)完成相应的动作. 运动控制系统的典型构成 1. ...

  6. 在Razor中输出Html的两种方式

    Razor中所有的Html都会自动编码,这样就不需要我们手动去编码了(安全),但在需要输出Html时就是已经转义过的Html文本了,如下所示: @{ string thisTest = "& ...

  7. linux ssh

    SSH 是建立在应用层和传输层基础上的一种安全协议. SSH传输数据是加密的,可以有效防止传输过程被截取数据保障安全. SSH的数据是经过压缩的,所以可以加快传输的速度 1. 首先查看一下当前linu ...

  8. sringboot项目在tomcat上的部署

    sringboot项目在tomcat上的部署原文链接: https://blog.csdn.net/zhaoyahui_666/article/details/78283559#comments 20 ...

  9. 小型IT部门建设之我见

    ​​关于IT团队建设,一般的原则是四大块组成: 1:业务分析师,对接业务部门,把业务需求转变成技术需求               专注于BRS( Business Requirement Speci ...

  10. 如何给Elasticsearch安装中文分词器IK

    安装Elasticsearch安装中文分词器IK的步骤: 1. 停止elasticsearch 2.2的服务 2. 在以下地址下载对应的elasticsearch-analysis-ik插件安装包(版 ...