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. C#修改文件名方法

    static void Main(string[] args) { string srcFileName = @"c:\order.txt"; string destFileNam ...

  2. MYSQL的联合查询最好是少用,效能差异巨大

    同样的功能,不同的写法,时间和内存占用差了几千倍,不废话,直接上代码 第一种写法: 代码如下: $Rs=DB::get($_ENV['DB'],3,"SELECT * FROM _xiazh ...

  3. ABAP表生成Java实体Bean

    项目中需要将HR模块中的表数据同步到Java系统中,向外围系统提供分发与查询服务,涉及到的表有两百多张,字段好几千上万个,如果手工一张张这些ABAP表在Java系统数据库中创建一遍的话,工作量将非常大 ...

  4. angular 2 - 006 change detection 脏治检查 - DC

    ANGULAR CHANGE DETECTION EXPLAINED 引发脏治检查有三种方式: Events - click, submit, - XHR - Fetching data from a ...

  5. TensorFlow保存和载入模型

    首先定义一个tf.train.Saver类: saver = tf.train.Saver(max_to_keep=1) 其中,max_to_keep参数设定只保存最后一个参数,默认值是5,即保存最后 ...

  6. (20170207)开源第三方学习之JSONModel

    1:仓库地址:https://github.com/jsonmodel/jsonmodel   主要作用是把JSON字符串转成Model实体,也可以把实体转化成JSON字符串:还包含一些转字典的内容: ...

  7. Ubuntu18.04下可以完美运行Quake3 Arena

    安装 其实很早就知道Linux下面可以跑Quake3, 但是一直没有付诸行动, 在硬盘上躺了很多年的Quake III Arena, 和Brood一起从来不舍得删, 昨天终于想起来试试. 安装很简单, ...

  8. np.memmap读取大文件

    Numpy中的ndarray是一种新形式的Python内建类型.因此,它可以在需要时被继承.ndarray形成了许多有用类的基础. np.memmap就是其中一种,它是内存映射文件.本质上就是使用C语 ...

  9. 日志分析工具 Log Parser

    微软的Log Parser, 下载地址 https://www.microsoft.com/en-us/download/details.aspx?id=24659 支持多种文件格式的分析,输入输出, ...

  10. 【Linux】CentOs的常用命令

    1.文件目录操作命令            ls:列出当前路径下的文件或者目录列表                -l 列出详细信息                -a 列出所有的文件信息,包括隐藏文 ...