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. pat1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  2. 如何处理HTML标签属性

    在jQuery里我们可以通过.attr()的方法来实现对HTML标签属性(tag attribute)处理. 1. 获取标签属性的值 (演示) 语法:$('选定目标').attr('属性名') 例子如 ...

  3. Oracle使用jdbc调用带游标参数的存储过程

    package com.jckb.procedure; import java.sql.CallableStatement; import java.sql.Connection; import ja ...

  4. iOS多线程系统整理 swift

    多线程   是一个应用程序内多个代码的执行路径,执行线程,同时在同一时间里执行不同的任务. 三种: 1.NSTread 2.Cocoa NSOperation (NSOperation,NSOpera ...

  5. 为什么要使用Vuex?

    为什么要使用Vuex? 1. 假如不使用 1.1 父子组件依赖同一个state 1.2 兄弟组件依赖同一个state 2. 用了Vuex之后 3. 方便记忆和理解

  6. vue使用element-ui实现按需引入

    基于Vue的Ui框架 饿了么公司基于vue开的的vue的Ui组件库 Element Ui 基于vue pc端的UI框架 MintUi 基于vue 移动端的ui框架 http://element.ele ...

  7. ELF interpreter /libexec/ld-elf32.so.1 not found

    ELF interpreter /libexec/ld-elf32.so.1 not found错误, 其实就是在64位平台上运行32位软件的不兼容造成的.找个64的包安装就上ok了.

  8. Linux常用命令-1

    内部命令:属于Shell解释器的一部分(已调入内存) 外部命令:独立于Shell解释器之外的程序文件(在磁盘上) 获得命令帮助 1)内部命令help 查看Bash内部命令的帮助信息 2)命令的“--h ...

  9. 又一例网卡mtu值引发的问题

    通过php上传文件到云存储,很小的文件都无法上传,在别的服务器上测试可以,本机环境是ESXI虚机安装的centos 7版本 解决思路过程 1.让开发写一个单独测试上传的文件,不调php nginx配置 ...

  10. python_11_guess任性玩

    age_of_oldboy=56 count=0 while count<3: guess_age=int(input("guess age:")) if guess_age ...