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. Linux终端记录神器

    我们在调试程序的时候,免不了要去抓一些 log ,然后进行分析.如果 log 量不是很大的话,那很简单,只需简单的复制粘贴就好.但是如果做一些压力测试,产生大量 log ,而且系统内存又比较小(比如嵌 ...

  2. Android性能优化-App启动优化

    原文地址:https://developer.android.com/topic/performance/launch-time.html#common 通常用户期望app响应和加载速度越快越好.一个 ...

  3. 快速准备(复制替换)一套新测试环境,CentOS7 MySQL相关配置

    拿到一个新环境,需要找相关配置,我有一个办法,相对能比较快速地复制一套环境出来. 修改机器配置: virsh 相关几条命令,已完成,后续我再整理补充... 虚拟化相关,参考:https://www.c ...

  4. Jquery实现日期转换为 Unix时间戳及时间戳转换日期

    (function ($) { $.extend({ myTime: { /** * 当前时间戳 * @return <int> unix时间戳(秒) */ CurTime: functi ...

  5. Swift 与 C 语言混合编程

    前言 作为一种可与 Objective-C 相互调用的语言,Swift 也具有一些与 C 语言的类型和特性,如果你的代码有需要,Swift 也提供了和常见的 C 代码结构混合编程的编程方式. 1.基本 ...

  6. WEBAPI 的简单示例

    一.webapi 1.在webapiconfig中移除xml的返回格式,返回格式就自动使用Json格式 config.Formatters.Remove(config.Formatters.XmlFo ...

  7. GuavaCache学习笔记三:底层源码阅读

    申明:转载自 https://www.cnblogs.com/dennyzhangdd/p/8981982.html 感谢原博主的分享,看到这个写的真好,直接转载来,学习了. 另外也推荐另外一篇Gua ...

  8. gitlab 502 报错

    这里从网上查到文章,我这里看了一下我这里是unicorn的问题 说一下情况:这里我们的一个前端修改了大量的打包,并进行了打包.然后提交merge request  分支到master,结果看到页面50 ...

  9. android 对话框全屏

    对话框风格 <style name="Lam.Dialog.FullScreen" parent="@style/Theme.AppCompat.Dialog&qu ...

  10. ThreadPoolExecutor 线程池浅析

    作为Executor框架中最核心的类,ThreadPoolExecutor代表着鼎鼎大名的线程池,它给了我们足够的理由来弄清楚它. 下面我们就通过源码来一步一步弄清楚它. 内部状态 线程有五种状态:新 ...