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 给你块地,有空地,也有草堆,让你选两个草堆进行点火,燃烧的草堆会引燃上下左右的相邻草堆,每一次引燃花费1s时间,问你最少花多长时间把草堆都点着,如果做不到输出-1.
这个题一开始姿势不对,想错了,先bfs下找连通块,如果连通块个数大于3直接GG,否则再在已知连通块内求个深度........283行代码直接挂掉了。
然而正确思路是酱紫的:及时有只一个连通块,我们也可以选择两个点火点来减少时间。
所以直接暴力枚举每两个草堆,把这两个点加入bfs队列,两起点bfs,看看此时能点多少点多少的bfs的时间(就是q中最后一个被pop出的元素的depth),再判断下选这两个点是否能把草堆全点着。
PS:这题最后半小时不过是因为没有初始化,以后养成好习惯每次都在每个测例运行前加一行init()......
代码如下:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; char a[][];
int b[][];
int x[],y[];
int to[][]={{,},{,},{-,},{,-}}; struct Node{
int x,y,s;
}node; int main()
{
int t,tt,n,m,c1,c2,cc,ans,i,j;
queue<Node> q;
scanf("%d",&t);
tt=t;
while(t--){
scanf("%d%d",&n,&m);
c1=;cc=;ans=;
memset(x,,sizeof(x));
memset(y,,sizeof(y));
for(i=;i<n;i++){
getchar();
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='#'){
c1++;
x[++cc]=i;
y[cc]=j;
}
}
}
for(int ii=;ii<=cc;ii++){
for(int jj=;jj<=cc;jj++){
memset(b,,sizeof(b));
node.x=x[ii];
node.y=y[ii];
b[node.x][node.y]=;
node.s=;
q.push(node);
node.x=x[jj];
node.y=y[jj];
b[node.x][node.y]=;
node.s=;
q.push(node);
if(ii==jj) c2=;
else c2=;
while(q.size()){
for(i=;i<;i++){
int tx=q.front().x+to[i][];
int ty=q.front().y+to[i][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(a[tx][ty]=='#'&&b[tx][ty]==){
c2++;
b[tx][ty]=;
node.x=tx;
node.y=ty;
node.s=q.front().s+;
q.push(node);
}
}
q.pop();
}
if(c1==c2){
if(node.s<ans) ans=node.s;
}
}
}
if(ans==) ans=-;
printf("Case %d: %d\n",tt-t,ans);
}
return ;
}

FZU 2150 Fire Game (高姿势bfs--两个起点)(路径不重叠:一个队列同时跑)的更多相关文章

  1. fzu 2150 Fire Game 【身手BFS】

    称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...

  2. FZU 2150 Fire Game (暴力BFS)

    [题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...

  3. FZU 2150 Fire Game 【两点BFS】

    Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...

  4. FZU 2150 Fire Game(点火游戏)

    FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description - 题目描述 ...

  5. (FZU 2150) Fire Game (bfs)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...

  6. FZU 2150 fire game (bfs)

    Problem 2150 Fire Game Accept: 2133    Submit: 7494Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  7. FZU 2150 Fire Game(双起点)【BFS】

    <题目链接> 题目大意: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一 ...

  8. FZU - 2150 Fire Game bfs+双起点枚举

    题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地.有墙壁‘·‘阻挡.初始可以从任意两点点火.问烧完最短的时间.若烧不完输出-1. 题解:由于100的数据量,直接暴力. ...

  9. FZU 2150 Fire Game

    Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  10. FZU 2150 Fire Game 广度优先搜索,暴力 难度:0

    http://acm.fzu.edu.cn/problem.php?pid=2150 注意这道题可以任选两个点作为起点,但是时间仍足以穷举两个点的所有可能 #include <cstdio> ...

随机推荐

  1. angularJS contenteditable 指令双向绑定

    项目遇到需求有点奇葩:双击div使其可编辑,失去焦点后进行数据绑定 通过自定义指令完成 好了上代码: .directive('contentEditable', function() { return ...

  2. source insight 配置小记

    0/ Alt + T 打开 Document Options,设置字体,添加 C++ 类型文件 .cc , 删除 Plain txt 类型以避免添加 .txt 文件 1/ Alt + F12 切换函数 ...

  3. Spring与JDK版本不一致引发问题Caused by: java.lang.IllegalArgumentException

    tomcat启动一个spring的项目,tomcat使用8.5,JDK使用1.8,Spring使用3.0,启动之后报错 Caused by: java.lang.IllegalArgumentExce ...

  4. 设置netbeans文件编码格式

    在项目ecmall上右键 选择属性,然后在项目属性里设置

  5. xcode升级到6.0以后遇到的警告错误解决方法

    Xcode 升级后,常常遇到的遇到的警告.错误,解决方法 从sdk3.2.5升级到sdk 7.1中间废弃了很多的方法,还有一些逻辑关系更加严谨了.1,警告:“xoxoxoxo”  is depreca ...

  6. EasyDarwin流媒体服务器RTSP拉模式流媒体转发模块设计

    拉模式转发 拉模式转发,顾名思义就是服务器主动从源端(IPCamera.NVR.或者其他流媒体服务器)通过RTSP/RTP协议将流媒体音视频数据拉取到流媒体转发服务器,再通过内部分发调度机制,分发给请 ...

  7. jsp页面中文乱码解决方案

    一.JSP页面中文乱码 在JSP页面中,中文显示乱码有两种情况:一种是HTML中的中文乱码,另一种是在JSP中动态输出的中文乱码. 先看一个JSP程序: <%@ page language=&q ...

  8. wifi 协议栈的历史的总结

    google 了一下找到下面的网页关于wifi 协议栈的说明 https://www.lifewire.com/wireless-standards-802-11a-802-11b-g-n-and-8 ...

  9. 基本动画、复合动画设置 平移、缩放、旋转、透明度 编码实现 xml实现

    public class VAActivity extends Activity { private ImageView iv_animation; private TextView tv_anima ...

  10. Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs

    题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...