Problem 2150 Fire Game

Accept: 3772    Submit: 12868
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
 
题意:给了你一块草地,'#'表示地上有草,火可以蔓延,'.'火不可以蔓延,然后有两个吃的没事干的人分别在这个草地上的某一个地方点火,点火的时间不算,之后每一秒火都会蔓延到其上下左右,问可以不可烧完所有的草,可以就输出最少的时间,不可以就输出-1
思路:只能枚举每两个草了,所以题目给的地也不是很大,也就10*10最大了,之后就每两个点一起烧,一开始队列里面要放进去两个点,之后可以烧到的草有可能两把火都会被烧到,所以应该取较早时间被烧到的那个时间,但如果火都烧没了,不能蔓延了却还有草,说明不能烧完所有的草,那就输出-1
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define ll long long
int const maxn = ;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
}
char maze[maxn][maxn];
int ans;
struct point
{
int x,y;
};
int n,m;
int dis[maxn][maxn];
queue<point>que;
int dx[]={,,-,};
int dy[]={,,,-};
int bfs(int x1,int y1,int x2,int y2)
{
while(!que.empty())
que.pop();
memset(dis,INF,sizeof(dis));
point p1,p2,next;
p1.x=x1;
p1.y=y1;
p2.x=x2;
p2.y=y2;
dis[x1][y1]=;
dis[x2][y2]=;
que.push(p1);
que.push(p2);
while(!que.empty())
{
point p=que.front();
que.pop();
for(int i=;i<;i++)
{
int nx=p.x+dx[i];
int ny=p.y+dy[i];
if(nx>= && nx<n && ny>= && ny<m && maze[nx][ny]=='#' && dis[nx][ny]>dis[p.x][p.y]+)
{
dis[nx][ny]=dis[p.x][p.y]+;
next.x=nx;
next.y=ny;
que.push(next);
}
}
}
int maxx=;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(maze[i][j]=='#')
maxx=max(maxx,dis[i][j]);
return maxx;
}
int main()
{
int t; scanf("%d",&t);
int ca=;
while(t--)
{
int ans=INF;
scanf("%d %d",&n,&m);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
cin>>maze[i][j];
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(maze[i][j]=='#')
{
for(int ii=;ii<n;ii++)
{
for(int jj=;jj<m;jj++)
{
if(maze[ii][jj]=='#')
{
int temp=bfs(i,j,ii,jj);
ans=min(ans,temp);
}
}
}
}
}
}
if(ans==INF)
ans=-;
printf("Case %d: %d\n",ca++,ans);
}
}

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

  1. Fire Again CodeForces - 35C (BFS)

    After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows ...

  2. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  3. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  4. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  5. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  6. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  7. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  8. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  9. 层层递进——宽度优先搜索(BFS)

    问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...

随机推荐

  1. @property的4类修饰符

    一.读写性修饰符:readwrite | readonly readwrite:表明这个属性是可读可写的,系统为我们创建这个属性的setter和getter方法. readonly:表明这个属性只能读 ...

  2. python_3 :用python微信跳一跳

    [学习使用他人代码] 2018年01月21日 19:29:02 独行侠的守望 阅读数:319更多 个人分类: Python 编辑 版权声明:本文为博主原创文章,转载请注明文章链接. https://b ...

  3. ArcGIS中Features与JSON的互相转化

    实际操作过程非常简单,这里就简单记录下转换工具的位置:

  4. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:0.概述

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 序言 帖主和队友仿制了一个简单版的微信,其中,队友是用Unity3D做前段,帖主用Java的Mina.Hiberna ...

  5. ElasticSearch 5学习(5)——第一个例子

    想要知道ElasticSearch是如何使用的,最快的方式就是通过一个简单的例子,第一个例子将会包括基本概念如索引.搜索.和聚合等,需求是关于公司管理员工的一些业务. 员工文档索引 业务首先需要存储员 ...

  6. mstsc远程桌面全频或自定义窗口

    近期发现自己的mstsc突然窗口变小,总是要繁琐的拖来拖去,现终于解决了,拿来分享一二! 参数一:mstsc /f   以全屏模式显示(操作一次,后面无需在设置,具体何原因要问微软了,可能是机器的分辨 ...

  7. coursera 算法二 week 1 wordnet

    这周的作业可谓是一波三折,但是收获了不少,熟悉了广度优先搜索还有符号图的建立.此外还知道了Integer.MAX_VALUE. SAP: 求v和w的大概思路是对v和w分别广度优先搜索,然后遍历图中每一 ...

  8. 配置Python环境变量

    虽然是老问题了,现在安装都自动配置环境变量. 这里,我是在VS2017中安装的Python3.6,但是没有自动配置好环境变量. 配置Python环境变量 打开[此电脑]—[属性]—[高级系统设置]—[ ...

  9. Java 虚拟机枚举 GC Roots 解析

    JVM 堆内存模型镇楼. 读<深入理解 Java 虚拟机>第三章GC算法,关于 GC Roots 枚举的段落没说透彻,理解上遇到困惑.因此对这点进行扩展并记录,发现国内各种博客写来写去都是 ...

  10. 用TreeView控件遍历磁盘目录

    实现效果: 知识运用: ListView控件中Items集合的Add方法  TteeView控件中Nodes集合的Add方法 实现代码: private void Form1_Load(object ...