FZU 2150 fire game (bfs)

时间:2023-02-23 21:35:22
Problem 2150 Fire Game

Accept: 2133    Submit: 7494
Time Limit: 1000 mSec    Memory Limit :
32768 KB

FZU 2150 fire game (bfs) 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.

FZU 2150 fire game (bfs) 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

FZU 2150 fire game (bfs) 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.

FZU 2150 fire game (bfs) Sample Input

4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#

FZU 2150 fire game (bfs) 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. cocos2d-x 帧动画

    ani = cc.Animation:create(); ...... local animate = cc.Animate:create(ani); s:runAction(animate); 发现 ...

  2. html meta标签使用总结

    meta标签作用 META标签是HTML标记HEAD区的一个关键标签,提供文档字符集.使用语言.作者等基本信息,以及对关键词和网页等级的设定等,最大的作用是能够做搜索引擎优化(SEO). PS:便于搜 ...

  3. concat&lpar;&rpar; 方法用于连接两个或多个数组。

    我们创建了三个数组,然后使用 concat() 把它们连接起来: <script type="text/javascript"> var arr = new Array ...

  4. bzoj4518&colon; &lbrack;Sdoi2016&rsqb;征途--斜率DP

    题目大意:把一个数列分成m段,计算每段的和sum,求所有的sum的方差,使其最小. 由方差*m可以化简得ans=m*sigma(ki^2)-sum[n]^2 很容易得出f[i][j]=min{f[i- ...

  5. &lbrack;问题2014A13&rsqb; 复旦高等代数 I(14级)每周一题(第十五教学周)

    [问题2014A13]  设 \(V\) 是数域 \(K\) 上的 \(n\) 维线性空间, \(\varphi\) 是 \(V\) 上的幂零线性变换且满足 \(\mathrm{r}(\varphi) ...

  6. ubuntu14 下配置cgi

    1.安装apache2 sudo apt-get install apache2 2.配置apache2 vim /etc/apache2/sites-enabled/ 000-default.con ...

  7. &lbrack;排序&rsqb; 快排 &amp&semi;&amp&semi; 冒泡(自己写)

    #include <iostream> using namespace std; /* 快速排序 通过一趟排序,以轴点为界 分割为两部分:左部分 <= 轴点 <= 右部分 再分 ...

  8. 使用Nginx搭建本地流媒体服务器

    Mac搭建nginx+rtmp服务器 1.打开终端,查看是否已经安装Homebrew,直接输入命令 man brew 如果Mac已经安装了, 会显示一些命令的帮助信息. 此时输入Q退出即可, 直接进入 ...

  9. MySQL 在线更改 Schema 工具

    MySQL在线更改schema的工具很多,如Percona的pt-online-schema-change. Facebook的 OSC 和 LHM 等,但这些都是基于触发器(Trigger)的,今天 ...

  10. C&num; 利用反射动态加载dll

    笔者遇到的一个问题,dll文件在客户端可以加载成功,在web端引用程序报错.解决方法:利用反射动态加载dll 头部引用加: using System.Reflection; 主要代码: Assembl ...