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+ which refers to the grid (x+, y), (x-, y), (x, y+), (x, y-). 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.

 <= T <=,  <= n <=,  <= m <=

 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 -. See the sample input and output for more details.

 Sample Input


.#.
###
.#. .#.
#.#
.#. ...
#.#
... ###
..#
#.#

 Sample Output

Case :
Case : -
Case :
Case :

暴力枚举两点,bfs统计,最后求最小值。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 16
#define inf 1<<26
int n,m;
char mp[N][N];
int vis[N][N];
int dirx[]={,,-,};
int diry[]={-,,,};
struct Node{
int x,y;
int t;
}node[N*N]; int bfs(Node a,Node b){
queue<Node>q;
q.push(a);
q.push(b);
vis[a.x][a.y]=;
vis[b.x][b.y]=;
int T=;
while(!q.empty()){
Node tmp=q.front();
q.pop();
Node cnt;
T=max(T,tmp.t);
for(int i=;i<;i++){
cnt.x=tmp.x+dirx[i];
cnt.y=tmp.y+diry[i];
if(cnt.x< || cnt.x>=n || cnt.y< || cnt.y>=m) continue;
if(vis[cnt.x][cnt.y]) continue;
if(mp[cnt.x][cnt.y]=='.') continue;
vis[cnt.x][cnt.y]=;
cnt.t=tmp.t+;
q.push(cnt);
}
}
return T;
} int main()
{
int t,ac=;
scanf("%d",&t);
while(t--){
int num=;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++){
if(mp[i][j]=='#'){
node[num].x=i;
node[num].y=j;
node[num++].t=;
}
}
} printf("Case %d: ",++ac);
if(num<=){
printf("0\n");
continue;
} int ans=inf;
for(int i=;i<num;i++){
for(int j=i;j<num;j++){
int flag=;
memset(vis,,sizeof(vis));
int w=bfs(node[i],node[j]);
//printf("===%d\n",w);
for(int k=;k<n;k++){
for(int l=;l<m;l++){
if(mp[k][l]=='#' && vis[k][l]==){
flag=;
break;
}
}
if(flag==){
break;
}
}
if(flag){
ans=min(ans,w);
}
}
} if(ans!=inf){
printf("%d\n",ans);
}
else{
printf("-1\n");
}
}
return ;
}

foj 2150 Fire Game(bfs暴力)的更多相关文章

  1. FZU 2150 Fire Game (暴力BFS)

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

  2. (FZU 2150) Fire Game (bfs)

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

  3. FZU 2150 Fire Game (bfs+dfs)

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

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

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

  5. fzu 2150 Fire Game 【身手BFS】

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

  6. FZU 2150 fire game (bfs)

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

  7. FZU Problem 2150 Fire Game

    Problem 2150 Fire Game Accept: 145    Submit: 542 Time Limit: 1000 mSec    Memory Limit : 32768 KB P ...

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

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

  9. FZOJ Problem 2150 Fire Game

                                                                                                        ...

随机推荐

  1. instsrv.exe下载和使用方法(微软Windows Server 2003 Resource Kit Tools工具中的一个)

    instsrv.exe是微软Windows Server 2003 Resource Kit Tools工具中的一个. Windows Server 2003 Resource Kit Tools是一 ...

  2. perl tk说明

    介绍: perl/Tk(也被称为pTK) 是一个模块和代码的收集,尝试 简单的配置Tk 8 部件工具包到强大的词素文文字, 动态内存,I/O, 和面向对象,它是一种解释脚本语言 来制作部件和程序 使用 ...

  3. 十个JAVA程序员容易犯的错误

    十个JAVA程序员容易犯的错误 1. Array 转 ArrayList 一般开发者喜欢用: List<String> list = Arrays.asList(arr); Arrays. ...

  4. poj 3230 Travel(dp)

    Description One traveler travels among cities. He has to pay for this while he can get some incomes. ...

  5. C# 将XML转换成DataSet【转】

    XmlDocument xml = new XmlDocument();xml.LoadXml(str); //str:具有xml格式的字符串 XmlNodeReader reader = new X ...

  6. Android自定义UI的实现和应用

    在Android项目开发中,不可避免的要遇到自定义的UI,用较好的体验去讨好UED妹子和交互设计师手下留情~几个迭代下来,遇到了不少这样的要求,有简单有复杂.最好的实现方案就是讲业务和UI隔离,封装成 ...

  7. jQuery ZeroClipboard中Flash定位不准确的解决方案

    转自波斯马,原文地址<jQuery ZeroClipboard中Flash定位不准确的解决方案> jQuery ZeroClipboard支持在多种浏览器中复制内容到剪贴板,IE.Fire ...

  8. 移动端影像解决方案Adobe Creative SDK for ios

    移动端影像解决方案Adobe Creative SDK for ios 2015-12-20 分类:整理 阅读(390) 评论(0)  老牌影像界泰斗不甘落寞,正式推出了Adobe Creative ...

  9. vuejs 三级联动

    最近在学习vuejs,写了一个城市三级联动效果,可以用在项目中的收获地址管理,支持新增与修改操作 HTML <script src="https://npmcdn.com/vue/di ...

  10. A==?B(A,B超级大)

    #include <iostream>#include <string.h>#include <cstring>using namespace std;struct ...