FZU 2150 fire game (bfs)
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
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#
Sample Output
Case 2: -1
Case 3: 0
Case 4: 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)的更多相关文章
- FZU 2150 Fire Game(BFS)
点我看题目 题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的.问你最少花多少时间 ...
- FZU Problem 2150 Fire Game(bfs)
这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...
- FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
- FZU 2150 Fire Game (暴力BFS)
[题目链接]click here~~ [题目大意]: 两个熊孩子要把一个正方形上的草都给烧掉,他俩同一时候放火烧.烧第一块的时候是不花时间的.每一块着火的都能够在下一秒烧向上下左右四块#代表草地,.代 ...
- 【FZU - 2150】Fire Game(bfs)
--> Fire Game 直接写中文了 Descriptions: 两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地 ...
- 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 ...
- 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 ...
- foj 2150 Fire Game(bfs暴力)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...
- Fire Game--FZU2150(bfs)
http://acm.fzu.edu.cn/problem.php?pid=2150 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=659 ...
随机推荐
- 使用C#版Tesseract库
上一篇介绍了Tesseract库的使用(OCR库Tesseract初探),文末提到了Tesseract是用c/c++开发的,也有C#的开源版本,本篇介绍一下如何使用C#版的Tesseract. C#版 ...
- git图解
- R文本挖掘之jiebaR包
library(jiebaRD)library(jiebaR) ##调入分词的库cutter <- worker()mydata =read.csv(file.choose(),fileEnc ...
- [转]你可能不知道的五个强大HTML5 API
一.全屏 // 找到适合浏览器的全屏方法 function launchFullScreen(element) { if(element.requestFullScreen) { element.re ...
- golang sync包
sync 在golang 文档上,golang不希望通过共享内存来进行进程间的协同操作,而是通过channel的方式来进行,当然,golang也提供了共享内存,锁等机制进行协同操作的包: 互斥锁: M ...
- 【C++】C++中的函数
目录结构: contents structure [-] 简介 可变形参的函数 initializer_list形参 省略符形参 main函数处理命令行选项 函数指针与函数引用 inline内联函数 ...
- mybatis 中一对多、多对一、多对多、父子继承关系
mybatis 中处理一对多.多对一.多对多.父子继承关系的有关键词:association .collection .discriminator id – 一个 ID 结果:标记出作为 ID 的结果 ...
- Could not resolve all dependencies for configuration ':classpath'
我这里是copy过来的项目包名没有修改,导致依赖找不到
- 刨根问底 | Elasticsearch 5.X集群多节点角色配置深入详解【转】
转自:https://blog.csdn.net/laoyang360/article/details/78290484 1.问题引出 ES5.X节点类型多了ingest节点类型. 针对3个节点.5个 ...
- C语言 · 年龄巧合
标题:年龄巧合 小明和他的表弟一起去看电影,有人问他们的年龄.小明说:今年是我们的幸运年啊.我出生年份的四位数字加起来刚好是我的年龄.表弟的也是如此.已知今年是2014年,并且,小明说的年龄指的是周岁 ...