Problem 2150 Fire Game

Accept: 2133    Submit: 7494
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
 
感觉bfs很容易错在细节上,一定要注意细节!
刚开始写了一个队列的,后来发现需要两个队列。。。结果又发现一个队列就够了!
另外要注意特判grass数量<=2的情况;
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f using namespace std; struct node{
int a,b;
int step;
};
node que1[];
int start1=,endd1=;
node grass[];
int couGrass=;
char ch[][];
int directX[]={-,,,};
int directY[]={,,-,};
int t,n,m; int bfs(int first,int second,int cougra){
int nowCouGra=;
int nowstep=;
start1=,endd1=;
char vis[][];
for(int i=;i<n;i++){
for(int j=;j<m;j++){
vis[i][j]=ch[i][j];
}
}
node t1,t2;
t1.a=grass[first].a;
t1.b=grass[first].b;
t1.step=;
t2.a=grass[second].a;
t2.b=grass[second].b;
t2.step=;
que1[endd1++]=t1;
que1[endd1++]=t2;
vis[t1.a][t1.b]='.';
vis[t2.a][t2.b]='.';
nowCouGra++,nowCouGra++;
while(start1<endd1){
node now=que1[start1++];
for(int i=;i<;i++){
node next;
next.a=now.a+directX[i];
next.b=now.b+directY[i];
next.step=now.step+;
if(next.a>=&&next.a<n&&next.b>=&&next.b<m){
if(vis[next.a][next.b]=='#'){
vis[next.a][next.b]='.';
que1[endd1++]=next;
nowstep=max(nowstep,next.step);
nowCouGra++;
}
}
}
if(nowCouGra==cougra){
return nowstep;
}
}
return INF;
} int main()
{
scanf("%d",&t);
for(int ii=;ii<t;ii++){
couGrass=;
scanf("%d %d",&n,&m);
getchar();
for(int j=;j<n;j++){
for(int k=;k<m;k++){
scanf("%c",&ch[j][k]);
if(ch[j][k]=='#'){
grass[couGrass].a=j;
grass[couGrass++].b=k;
}
}
getchar();
}
if(couGrass<=){
printf("Case %d: 0\n",ii+);
continue;
}
int ans=INF;
for(int i=;i<couGrass;i++){
for(int j=i+;j<couGrass;j++){
ans=min(ans,bfs(i,j,couGrass));
}
}
if(ans==INF){
printf("Case %d: -1\n",ii+);
}else{
printf("Case %d: %d\n",ii+,ans);
} }
return ;
}

FZU 2150 fire game (bfs)的更多相关文章

  1. FZU 2150 Fire Game(BFS)

    点我看题目 题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的.问你最少花多少时间 ...

  2. FZU Problem 2150 Fire Game(bfs)

    这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...

  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)

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

  5. 【FZU - 2150】Fire Game(bfs)

    --> Fire Game 直接写中文了 Descriptions: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地 ...

  6. FZU 2150 Fire Game (高姿势bfs--两个起点)(路径不重叠:一个队列同时跑)

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

  7. FZU 2150 Fire Game (高姿势bfs--两个起点)

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

  8. foj 2150 Fire Game(bfs暴力)

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

  9. Fire Game--FZU2150(bfs)

    http://acm.fzu.edu.cn/problem.php?pid=2150 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=659 ...

随机推荐

  1. 1、mysql初识

    之前我们写代码需要存取信息时用的是文件可是用文件存取数据非常局限,今天我们将走进一个新的世界mysql 本片导航: 数据库由来 数据库概述 mysql介绍 下载安装 mysql软件基本管理 初识sql ...

  2. ARMv8寄存器手册

    这份手册是从DS5里拷贝出来的,他是以网页的形式用浏览器打开,方面查看寄存器. ARMv8_regs_xhtml.tar.gz

  3. AOP - C# Fody中的方法和属性拦截

    很久很久以前用过postsharp来做AOP, 大家知道的,现在那东东需要付费,于是尝试了一下Fody,但是发现Fody跟新太快了,所以大家在安装fody的时候尽力安装老的版本:packages.co ...

  4. 【Geek软技能】程序员,为什么写不好一份简历?

    一份好简历会是一份好工作的开端. 为什么?沧海也会遗珠   简历,是如此重要,它是获得一份满意工作的敲门砖,但不同的简历敲门的声响可不同. 但很多时候简历给人的感觉也似乎微不足道,因为没有人会真正细致 ...

  5. [C#] 解决Silverlight反射安全关键(SecuritySafeCritical)时报“System.MethodAccessException: 安全透明方法 XXX 无法使用反射访问”的问题

    作者: zyl910 一.缘由 在Silverlight中使用反射动态访问时,经常遇到"System.MethodAccessException: 安全透明方法 XXX 无法使用反射访问-- ...

  6. linux 安装nvm,通过nvm安装node

    1,nvm git地址点击打开链接,安装命令 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh ...

  7. FtpCopy数据定时自动备份软件(FTP定时备份)

    1. 软件说明 FtpCopy是一款免费的FTP数据自动备份软件,如果FtpCopy对您有较大的帮助,欢迎捐赠我们,我们对您表示衷心的感谢! 如果有需求的话会一直更新下去,将软件做到极致! 有问题可直 ...

  8. SQL SERVER 中日期格式化,及GETDATE()、CONVERT()函数使用说明

    1. date和datetime类型的区别 date是SQL Server 2008新引进的数据类型.它表示一个日期,不包含时间部分,可以表示的日期范围从公元元年1月1日到9999年12月31日.只需 ...

  9. Clion使用MinGW编译好的boost库

    MinGW编译Boost库可以参考我之前写的编译Boost的文章. 以下是cmake链接boost静态库的配置: cmake_minimum_required(VERSION 3.8) project ...

  10. 使用idea对spring boot项目打jar和war包[文件]

    pom.xml文件编写 打JAR包时 <groupId>com.netcorner</groupId> <artifactId>demo</artifactI ...