Fire Game

Accept: 1955    Submit: 6880
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem 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
 //2017-02-28
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; char grid[][], tmp[][];
int dx[] = {, , , -};
int dy[] = {, -, , };
int book[][], answer = 0x3f3f3f3f;
struct node
{
int x, y, step;
}; void bfs(int n, int m, int x1, int y1, int x2, int y2)
{
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
tmp[i][j] = grid[i][j];
int x, y, step, ans = ;
tmp[x1][y1] = '*';
tmp[x2][y2] = '*';
queue<node> q;
node tmpnode;
tmpnode.x = x1;
tmpnode.y = y1;
tmpnode.step = ;
q.push(tmpnode);
tmpnode.x = x2;
tmpnode.y = y2;
q.push(tmpnode);
while(!q.empty())
{
x = q.front().x;
y = q.front().y;
step = q.front().step;
for(int i = ; i < ; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&tmp[nx][ny]=='#')
{
tmpnode.x = nx;
tmpnode.y = ny;
tmpnode.step = step+;
ans = step+;
tmp[nx][ny] = '*';
q.push(tmpnode);
}
}
q.pop();
}
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
if(tmp[i][j] == '#'){
return;
}
if(ans < answer)answer = ans;
} int main()
{
int T, n, m, cnt;
cin>>T;
for(int kase = ; kase <= T; kase++)
{
cin>>n>>m;
cnt = ;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
{
cin>>grid[i][j];
if(grid[i][j] == '#')cnt++;
}
answer = 0x3f3f3f3f;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
{
if(grid[i][j] == '#'){
for(int i2 = i; i2 < n; i2++)
for(int j2 = ; j2 < m; j2++){
if(i==i2 && j2 <= j)continue;
if(grid[i2][j2] == '#')bfs(n, m, i, j, i2, j2);
}
}
}
if(answer == 0x3f3f3f3f)answer = -;
if(cnt==)answer=;
cout<<"Case "<<kase<<": "<<answer<<endl;
}
return ;
}

FZU2150(KB1-I)的更多相关文章

  1. FZU2150 Fire Game —— BFS

    题目链接:https://vjudge.net/problem/FZU-2150 Problem 2150 Fire Game Accept: 2702    Submit: 9240 Time Li ...

  2. FZU2150 Fire Game BFS搜索

    题意:就是选两个点出发,只能走草坪,看能不能走完所有的草坪 分析:由于数据范围很小,所有枚举这两个点,事先将所有的草坪点存起来,然后任选两个点走,(两个点可以是同一个点) 然后BFS就行了 注:无解的 ...

  3. fzu2150(bfs)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 题意:在任意两处点火,求最短时间烧光所有草堆. 分析:由于n,m比较小,将所有草堆坐标记录下来,然后暴力 ...

  4. [宽度优先搜索] FZU-2150 Fire Game

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

  5. FZU-2150.FireGame.(BFS))

    本题大意:给出一个n * m的地,‘#’ 代表草, ‘.’代表陆地,每次选择这片地里的两片草,可选相等的草,选择的两片草初始状态为被燃状态,每一分钟被点燃的草会将身边的四连块点.问你需要对于给定的这片 ...

  6. FZU2150 :Fire Game (双起点BFS)

    传送门:点我 题意:“#”是草,"."是墙,询问能不能点燃俩地方,即点燃俩“#”,把所有的草烧完,如果可以,那么输出最小需要的时间,如果不行输出-1 思路:暴力BFS,看到n和m都 ...

  7. FZU2150 Fire Game

    题目: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一.求烧完所有的草需要的最少时间 ...

  8. 手把手教你玩转nginx负载均衡(三)----配置虚拟服务器网络

    引言 虽然上一篇我们成功的启动了虚拟机,也安装好了操作系统,但是这台虚拟机和主机以及其他虚拟机是没有办法连通的,我们的目标是配置多台服务器并且配置nginx反向代理,来实现负载均衡,所以不能访问内网是 ...

  9. 【Hello CC.NET】CC.NET 实现自动化集成

    一.背景 公司的某一金融项目包含 12 个子系统,新需求一般按分支来开发,测完后合并到主干发布.开发团队需要同时维护开发环境.测试环境.模拟环境(主干).目前面临最大的两个问题: 1.子系统太多,每次 ...

随机推荐

  1. Django 项目 实现重定向并进行反向解析

    一.项目路由配置urls.py中 from django.conf.urls import include, url from django.contrib import admin # 匹配应用 u ...

  2. C++类模板 template <class T>

    C++在发展的后期增加了模板(template )的功能,提供了解决这类问题的途径.可以声明一个通用的类模板,它可以有一个或多个虚拟的类型参数. 比如: class Compare_int class ...

  3. tomcat 项目发布方式

    1.WEB应用的组成结构 开发web应用时,不同类型的文件有严格的存放规则,否则不仅会使web应用无法访问 还会导致web服务器自动报错. mail:web应用所在目录(该目录自定义) html,js ...

  4. tomcat JNDI Resource 配置

    最近公司的项目慢慢开始向Maven项目迁移, 部分配置文件公共组也帮我们做了些改动,其中在spring的applicationContext.xml中看到了数据连接bean存在两个,一个是jndi 一 ...

  5. vmware安装——CentOS-6.5和Mysql

    1.新建虚拟机 2.安装centos6.5 3.centos设置 查看网络 4.vmware设置网络连接 关闭selinux [root@china ~]# vim /etc/selinux/conf ...

  6. 【BZOJ2127】happiness 最小割

    题目大意:有一个$n\times m$的矩阵,矩阵的每个位置上有一个同学,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科与理科有着自己的喜悦 ...

  7. 【PaddlePaddle系列】Executor逐步训练模型

    前言 PaddlePaddle使用Trainer训练模型虽然直接了当,方便快捷,但是对于一些需要逐步训练的模型则比较麻烦.类似Tensorflow采用session.run的形式逐步训练模型,使得训练 ...

  8. Seqlist L 与 Seqlist *L的区别

     Seqlist L结构体变量,SeqlistInsert之后不可以带回新的插入数据.  Seqlist *L 是传结构体指针 用SeqlistInsert之后可以有新的插入.

  9. Android 开发工具类 10_Toast 统一管理类

    Toast 统一管理类: 1.短时间显示Toast: 2.长时间显示 Toast: 3.自定义显示 Toast 时间. import android.content.Context; import a ...

  10. 如何虚拟机里安装win7操作系统

    不多说,直接上干货! Windows Server 2003.2008.2012系统的安装 关于给电脑换系统,很多人会花钱去电脑店里换,或者是下载Ghost系统.但这些系统都不是微软原版的,制作者已经 ...