Description

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
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define INF 1e9
const int maxn=*+;
char a[][];
int ta[maxn],tb[maxn],book[][],nex[][]={{,},{,},{-,},{,-}};
struct point
{
int x,y,z;
point(int x=,int y=,int z=):x(x),y(y),z(z){}
};
int m,n; int BFS(point aa,point cc)
{
queue<point>que;
que.push(aa);que.push(cc);
book[aa.x][aa.y]=;book[cc.x][cc.y]=;
int step=;
while(!que.empty())
{
point temp=que.front();
que.pop(); step=max(step,temp.z);
for(int k=;k<=;k++)
{
point pp;
pp.x=temp.x+nex[k][];
pp.y=temp.y+nex[k][];
pp.z=temp.z+;
if(pp.x<||pp.x>=m||pp.y<||pp.y>=n) continue;
if(book[pp.x][pp.y]||a[pp.x][pp.y]!='#') continue;
book[pp.x][pp.y]=;
que.push(pp);
}
}
return step;
} int main()
{
int T,ca=;
cin>>T;
while(T--)
{
cin>>m>>n;
int k=;
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
cin>>a[i][j];
if(a[i][j]=='#')
{
ta[k]=i;
tb[k++]=j;
}
}
cout<<"Case "<<ca++<<": ";
int ans=INF;
for(int i=;i<k;i++)
for(int j=i;j<k;j++)
{
memset(book,,sizeof(book));
int step=BFS(point(ta[i],tb[i],),point(ta[j],tb[j],));
int flag=;
for(int ki=;ki<m;ki++)
for(int kj=;kj<n;kj++)
if(a[ki][kj]=='#'&&book[ki][kj]==) {flag=;break;}
if(flag) {ans=min(step,ans);}
}
if(ans==INF) cout<<-<<endl;
else cout<<ans<<endl;
}
return ;
}

Final Battle #1 K题 Fire game的更多相关文章

  1. hdu 3986 Harry Potter and the Final Battle (最短路径)

    Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/6553 ...

  2. hdu5080:几何+polya计数(鞍山区域赛K题)

    /* 鞍山区域赛的K题..当时比赛都没来得及看(反正看了也不会) 学了polya定理之后就赶紧跑来补这个题.. 由于几何比较烂写了又丑又长的代码,还debug了很久.. 比较感动的是竟然1Y了.. * ...

  3. 2017Summmer_上海金马五校 F题,G题,I题,K题,J题

    以下题目均自己搜 F题  A序列 一开始真的没懂题目什么意思,还以为是要连续的子串,结果发现时序列,简直智障,知道题意之后,好久没搞LIS,有点忘了,复习一波以后,直接双向LIS,处理处两个数组L和R ...

  4. 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定理

    2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定 ...

  5. 2020牛客多校第八场K题

    __int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...

  6. 2019牛客暑期多校训练营(第四场)k题、j题

    传送门 k题: 题意: 给你一串由数字构成的字符串,你从这个字符串中找子字符串使这个字符串是300的倍数 题解: 这道题和第三场的B题极其相似 首先可以把是三百的倍数分开,必须要是100和3的倍数 是 ...

  7. ZOJ 3879 Capture the Flag 15年浙江省赛K题

    每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...

  8. 2016 ICPC青岛站---k题 Finding Hotels(K-D树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over ...

  9. 2013 南京邀请赛 K题 yet another end of the world

    /** 大意:给定一组x[],y[],z[] 确定有没有两个不同的x[i], x[j] 看是否存在一个ID使得 y[i]<=ID%x[i]<=z[i] y[j]<=ID%x[j]&l ...

随机推荐

  1. Luogu P1233 木棍加工 【贪心/LIS】

    题目描述 一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的.棍子可以被一台机器一个接一个地加工.机器处理一根棍子之前需要准备时间.准备时间是这样定义的: 第一根棍子的准备时间为1分钟: 如果刚处理 ...

  2. #undef及其用法

    简    介 在后面取消以前定义的宏定义 在此程序中,我们将取消在先前程序中对预处理器的定义. #include <stdio.h> int main( void ) { #define ...

  3. Windows API函数大全

    WindowsAPI函数大全(精心总结) 目录 1. API之网络函数... 1 2. API之消息函数... 1 3. API之文件处理函数... 2 4. API之打印函数... 5 5. API ...

  4. Lightoj 1054 - Efficient Pseudo Code

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1054 题目大意: 给出n,m,问n^m的所有因子之和是多少? 解题思路: 补 ...

  5. Codeforces Round #459 (Div. 2)C. The Monster

    C. The Monster time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. rabbitmq实践笔记(一):安装、配置与使用初探

    引言: 对于一个大型的软件系统来说,会有很多的组件.模块及不同的子系统一起协同工作,模块之间的通信需要一个可靠的通信管道来保证 ,通信管道需要解决解决很多问题,比如: 1)信息的发送者和接收者如何维持 ...

  7. java中实参与形参的概念

    形参: public void fun(形参类型 形参名){ ... } 实参: public static void main(String[] args){ 类 对象名=new 类(); 对象名. ...

  8. UML 顺序图(转载)

    顺序图精确表达用户与系统的复杂交互过程. 顺序图用于描述进出系统的信息流. 顺序图与协作图是同构的,可以互相转换!!! 顺序图:着重体现对象间消息传递的时间顺序.顺序图允许直观的表示出对象的生存期,生 ...

  9. 从源码对比DefaultServeMux 与 gorilla/mux

    从源码对比DefaultServeMux 与 gorilla/mux DefaultServeMux Golang自带的net/http库中包含了DefaultServeMux方法,以此可以搭建一个稳 ...

  10. R in action读书笔记(5)-第七章:基本统计分析

    7.1描述性统计分析 > vars<-c("mpg","hp","wt") > head(mtcars[vars])    ...