题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150

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
题目大意:两个人玩关于火的游戏,在一个地图上#表示干草,开始可以点燃两个干草(可重叠),火每秒可向周围四个方向蔓延,空白处不能过,问所有的干草是否能燃尽,如能,输出最快时间
思路:题中数据不大,把所有干草坐标统计一下,每次选出两个遍历一遍,统计所有可能的最小值
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define met(a,b) memset(a,b,sizeof(a))
#define N 109
int vis[N][N];
char str[N][N];
int dir[][]={{,},{-,},{,},{,-}};
int k,n,m;
struct node
{
int x,y,temp;
}a[N];///统计干草的坐标
int pan()///判断干草是否都能被点燃
{
int x,y;
for(int i=;i<k;i++)
{
x=a[i].x;y=a[i].y;
if(!vis[x][y])
return ;
}
return ;
}
int bfs(node a,node b)
{
int team=;
met(vis,);
queue<node>Q;
node q,p;
Q.push(a);Q.push(b);
vis[a.x][a.y]=;vis[b.x][b.y]=;
while(Q.size())
{
q=Q.front();
Q.pop();
for(int i=;i<;i++)
{
p.x=q.x+dir[i][];
p.y=q.y+dir[i][];
p.temp=q.temp+;
if(p.x>= && p.x<n&& p.y>= && p.y<m && str[p.x][p.y]=='#' && !vis[p.x][p.y])
{
vis[p.x][p.y]=;
team=max(team,p.temp);
Q.push(p);
}
}
}
if(pan())
return team;
else
return INF;
}
int main()
{
int t,con=;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(int i=;i<n;i++)
scanf("%s",str[i]);
k=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(str[i][j]=='#')
{
a[k].x=i;
a[k].y=j;
a[k++].temp=;
}
}
}
int ans=INF;
for(int i=;i<k;i++)
{
for(int j=i;j<k;j++)
{
ans=min(bfs(a[i],a[j]),ans);
}
}
printf("Case %d: ",con++);
if(ans==INF)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}
 

(FZU 2150) Fire Game (bfs)的更多相关文章

  1. 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 ...

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

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

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

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

  4. fzu 2150 Fire Game 【身手BFS】

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

  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)

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

  7. FZU 2150 Fire Game

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

  8. 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) ...

  9. Fire Game (FZU 2150)(BFS)

    题解:一开始想错了,以为只要烧完就是那个答案,但是这不是最优的结果,需要每两个点都bfs一遍,找到如果能够全部烧完,找到花费时间最小的,如果不能return -1.在bfs的时候,记录答案的方法参考了 ...

随机推荐

  1. Python:decorator [转]

    Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def sa ...

  2. June 15. 2018 Week 24th Friday

    If at first you don't succeed, then dust yourself off and try again. 失败了没关系,重振旗鼓,从头再来. From Aaliyah, ...

  3. voinc+vue实现级联选择

    需求: vonic中实现级联选择 <!DOCTYPE html> <html> <head> <title>下拉框</title> < ...

  4. (转)Spring Boot(八):RabbitMQ 详解

    http://www.ityouknow.com/springboot/2016/11/30/spring-boot-rabbitMQ.html RabbitMQ 即一个消息队列,主要是用来实现应用程 ...

  5. 本地项目上传到CODING

    1.在Coding上添加项目 1).新建项目 README:一般项目中都会添加一个README文件对项目进行概述,以便一目了然地知道这个项目是做什么用的,如何使用等信息.README文件采用markd ...

  6. mabatis insert into on duplicate key

    一.mabatis实现saveOrUpdate功能 <insert id="insert" parameterType="hystrixconfigdo" ...

  7. 用golang chromedp 操作已经打开的chrome浏览器

    win7 环境,主要是一开始想在代码中先用exec.Command启动chrome,但始终不能成功监听9222端口,折腾了很长时间, 需要先手工启动chrome监听端口,具体写在代码注释中了. 然后再 ...

  8. [matlab] 11.多边形凹凸性检测

    clear all;close all;clc; n=20; p=rand(n,2); p=createSimplyPoly(p); %创建简单多边形 hold on; for i=1:n if i= ...

  9. 在asp.net web api中利用过滤器设置输出缓存

    介绍 本文将介绍如何在asp.net web api中利用过滤器属性实现缓存. 实现过程 1,首先在web.config文件下appsettings下定义“CacheEnabled”和“CacheTi ...

  10. 牛客网训练1--------矩阵 (二份+二维矩阵hash)

    不懂hash的话:https://www.cnblogs.com/ALINGMAOMAO/p/10345850.html 思路:对于一个大矩阵的每一个子矩阵都对应着一个hash值k, 当k出现2次以上 ...