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 <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
#define inf 0x3f3f3f3f
int n,m;
bool vis[][];
char grid[][];
int casee=;
int ans=inf;
struct node
{
int x,y,depth;
};
vector <node>grass;
bool check (int x,int y)
{
if (!vis[x][y]&&grid[x][y]=='#'&&x>=&&x<n&&y>=&&y<m)
return true;
else
return false;
}
bool judge ()
{
for (int i=;i<n;++i){
for (int j=;j<m;++j){
if (grid[i][j]=='#'&&!vis[i][j])
return false;
}
}
return true;
}
void init()
{
grass.clear();
memset(vis,false,sizeof vis);
}
int bfs (node n1,node n2)
{
queue <node> q;
memset(vis,false,sizeof vis);
while (!q.empty()) q.pop();
q.push(n1);
q.push(n2);
int depthest=;
while (!q.empty())
{
node now=q.front();
q.pop();
if (vis[now.x][now.y])
continue;
vis[now.x][now.y]=true;
depthest=now.depth;
if (check(now.x-,now.y))
{
node nxt=now;
nxt.x--;
nxt.depth++;
q.push(nxt);
}
if (check(now.x+,now.y))
{
node nxt=now;
nxt.x++;
nxt.depth++;
q.push(nxt);
}
if (check(now.x,now.y-))
{
node nxt=now;
nxt.y--;
nxt.depth++;
q.push(nxt);
}
if (check(now.x,now.y+))
{
node nxt=now;
nxt.y++;
nxt.depth++;
q.push(nxt);
}
}
return depthest;
}
int main()
{
//freopen("de.txt","r",stdin);
int t;
scanf("%d",&t);
while (t--)
{
init();
ans=inf;
scanf("%d%d",&n,&m);
for (int i=;i<n;++i)
scanf("%s",grid[i]);
for (int i=;i<n;++i){
for (int j=;j<m;++j){
if (grid[i][j]=='#'){
node g;
g.x=i;
g.y=j;
g.depth=;
grass.push_back(g);
}
}
}
for (int i=;i<grass.size();++i)
{
for (int j=i;j<grass.size();++j)
{
grass[i].depth=;
grass[j].depth=;
int temp=min(bfs(grass[i],grass[j]),ans);
if (judge())
ans=min(ans,temp);
}
}
printf("Case %d: ",++casee);
if (ans==inf)
printf("-1\n");
else
printf("%d\n",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)

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

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

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

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

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

  8. FZU 2150 Fire Game

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

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

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

  10. FZU 2150 Fire Game (高姿势bfs--两个起点)(路径不重叠:一个队列同时跑)

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

随机推荐

  1. Oracle数据库之触发器(二)

    DML触发器是指在进行insert.update或delete操作时触发的程序体.如果你想在用户对数据进行操作时,记录或限制其操作,就可以用DML触发器.举例来说,我想统计我的网站用户的注册.注销或者 ...

  2. vue之条件语句小结

    vue之条件语句小结 v-if, v-else 随机生成一个数字,判断是否大于0.5,然后输出对应信息: <!DOCTYPE html> <html> <head> ...

  3. window安装oracle和创建数据库

    原文地址: https://www.cnblogs.com/hoobey/p/6010804.html  #11g安装 https://www.cnblogs.com/qq1272850043/p/6 ...

  4. ip地址与子网掩码----基础知识

    前言 IP地址有三种基本类型,由网络号的第一组数字来表示. A类地址的第一组数字为1-126. B类地址的第一组数字为128-191. C类地址的第一组数字为192-223. 注:数字0和 127不作 ...

  5. 2016年Esri技术公开课全年资料分享

    大家好,2016年的公开课活动在上周全部结束,感谢大家的支持. 2016年的公开课共进行20期,共有24位讲师参与,公开课视频播放.课件下载次数累计超10万次,在这里衷心的感谢大家的积极参与和分享精神 ...

  6. python分类预测模型的特点

    python分类预测模型的特点 模型 模型特点 位于 SVM 强大的模型,可以用来回归,预测,分类等,而根据选取不同的和函数,模型可以是线性的/非线性的 sklearn.svm 决策树 基于" ...

  7. 小程序中封装base64

    function Base64() { // private property var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn ...

  8. apt-cyg for Cygwin(setup-x86_64 .exe )在win10下的安装

    cygwin安装后,如果没有选择安装所有包(这会占用5G空间,很多包不需要),再需要安装新的包,可以启动setup-x86_64 .exe(我把它放置在C:\cygwin64目录下),添加包(如wge ...

  9. python3反转列表的三种方式

    1.内建函数 reversed() li = [1,2,3,4,5,6] a = list(reversed(li)) print(a) 注意:reversed()函数返回的是一个迭代器,而不是一个L ...

  10. appium常见问题05_修改Android手机运行环境(adb指令修改hosts)

    自动化测试过程中,手机有时会跳网,怎样保持手机测试的环境稳定性,可以通过adb指令修改android手机hosts,保持手机运行在hosts中配置的环境中: 修改方法如下: 前提条件:已安装andro ...