题目链接: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. Oracle完全复制表结构的存储过程

    最近在处理一个分表的问题时,需要为程序创建一个自动分表的存储过程,需要保证所有表结构,约束,索引等等一致,此外视图,存储过程,权限等等问题暂不用考虑. 在Mysql中,创建分表的存储过程,相当简单:c ...

  2. C#获取一个实体类的属性名称、属性值

    using System.Reflection; Type t = obj.GetType();//获得该类的Type foreach (PropertyInfo pi in t.GetPropert ...

  3. c/c++ 智能指针 weak_ptr 使用

    智能指针 weak_ptr 使用 weak_ptr用途: 1,解决空悬指针问题 2,解决循环引用问题 weak_ptr特点:没有*操作和->操作 weak_ptr是不控制所指对象生存周期的智能指 ...

  4. 浏览器本地数据存储解决方案以及cookie的坑

    本地数据存储解决方案以及cookie的坑 问题: cookie过长导致页面打开失败 背景: 在公司的项目中有一个需求是打开多个工单即在同一个页面中打开了多个tab(iframe),但是需要在刷新时只刷 ...

  5. 关于phpstorm ftp目录乱码

    关于IIS FTP服务器汉字文件目录乱码问题:一般来说,IIS 服务器编码默认为GBK,而你的目录可能是UTF-8,将phpstorm的远程连接设置为GBK就OK了.记住服务器的编码,文件的编码要统一

  6. Shell and DOS

    long long ago 自己便想总结下shell命令以及dos常用的命令,毕竟实际实践中会经常用到,用的好的批处理或者shell脚本会事半功倍,好了,废话不多说,开始. shell echo [字 ...

  7. 《Java大学教程》—第22章 多线程程序

    22.2 进程(process):P551时间切片(time-slicing):处理器只是完成了一个任务的一部分工作,然后完成下一个任务的一部分工作,因为处理吕每次完成工作的时间都非常短,因此看起来这 ...

  8. qt designer设置界面是label中文字与文本框对齐设置

    往往在使用 qt designer布置界面时,添加的label和文本框中是直接从工具箱中拖进去的,由于每个控件尺寸大小不一,就会造成label中的文字相对于文本框比较较偏上,看下面未经调整的直接效果 ...

  9. 简单的C#TCP协议收发数据示例

    参考:http://www.cnblogs.com/jzxx/p/5630516.html 一.原作者的这段话很好,先引用一下: Socket的Send方法,并非大家想象中的从一个端口发送消息到另一个 ...

  10. 004_浅析Python的GIL和线程安全

    在这里我们将介绍Python的GIL和线程安全,希望大家能从中理解Python里的GIL,以及GIL的前世今生. 对于Python的GIL和线程安全很多人不是很了解,通过本文,希望能让大家对Pytho ...