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. MYSQL 配置远程连接

    例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话.  GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'IDENTIFIED BY ...

  2. IEEE Trans 2006 使用K-SVD构造超完备字典以进行稀疏表示(稀疏分解)

    K-SVD可以看做K-means的一种泛化形式,K-means算法总每个信号量只能用一个原子来近似表示,而K-SVD中每个信号是用多个原子的线性组合来表示的.    K-SVD算法总体来说可以分成两步 ...

  3. 【洛谷2304_LOJ2134】[NOI2015]小园丁与老司机(动态规划_网络流)

    题目: 洛谷 2304 LOJ 2134 (LOJ 上每个测试点有部分分) 写了快一天 -- 好菜啊 分析: 毒瘤二合一题 -- 注意本题(及本文)使用 \(x\) 向右,\(y\) 向上的「数学坐标 ...

  4. 构造 HDOJ 5400 Arithmetic Sequence

    题目传送门 题意:问有多少个区间,其中存在j使得ai + d1 == ai+1(i<j) && ai + d2 == ai+1 (i>j) 构造:用c1[i], c2[i] ...

  5. 转 ORACLE-016:ora-01720 授权选项对于'xxxx'不存在

    报错的情形如下, A用户:视图V_A B用户:视图V_B,并且用到了V_A C用户:需要用V_B, 授权过程, A用户下: grant select on V_A to B B用户下: grant s ...

  6. 435 Non-overlapping Intervals 无重叠区间

    给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠.注意:    可以认为区间的终点总是大于它的起点.    区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠.示例 ...

  7. Apollo源码搭建调试看一文就够

    Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用于微服务配置管理场景. 我 ...

  8. AJPFX分析int 和integer的区别

    int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为int提供的封装类.int的默认值为0,而Integer的默认值为null,即Integer可 ...

  9. Android IJKPlayer缓冲区设置以及播放一段时间出错解决方案

    IJKPlayer拖动播放进度会导致重新请求数据,未使用已经缓冲好的数据,所以应该尽量控制缓冲区大小,减少不必要的数据损失. mMediaPlayer.setOption(IjkMediaPlayer ...

  10. 开发小Tips

    Kotlin语言篇: 1.抽象类的定义 abstract class Person(var name : String, var age : Int) : Any() { abstract var a ...