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 ...
随机推荐
- pygame 笔记-2 模仿超级玛丽的弹跳
在上一节的基础上,结合高中物理中的匀加速直线运动位移公式 ,就能做出类似超级玛丽的弹跳效果. import pygame pygame.init() win = pygame.display.set_ ...
- win32下使用相对exe文件的绝对路径资源
在使用VC++进行开发时,如果按F5进行Debug时,当前相对资源是相对工程的vcxproj的文件夹目录,而直接双击运行exe时,资源是相对exe的文件夹目录.为了兼容这二者,最好使用绝对路径,这样无 ...
- 对指针和引用的理解(c++)
1.指针 typedef说明一种新类型名,来代替已有类型名. a.案例:typedef char* String_t和#define String_d char *这两句在使用上的区别? 1)前者声明 ...
- windows ip 缓存清理
ip缓存 ipconfig /release dns缓存 ipconfig/flushdns
- Java之线程池深度剖析
1.线程池的引入 引入的好处: 1)提升性能.创建和消耗对象费时费CPU资源 2)防止内存过度消耗.控制活动线程的数量,防止并发线程过多. 使用条件: 假设在一台服务器完成一 ...
- 树莓派raspberry pi配置
(1)国际化语言 树莓派初装系统之后,首次启动会出现“raspi-config”工具,如下图:(若不是初次启动,在命令模式下,请输入 sudo raspi-config 命令,即可调出此界面.若在图形 ...
- chrome插件离线包下载和安装
添加扩展一般会有个url https://chrome.google.com/webstore/detail/axure-rp-extension-for-ch/dogkpdfcklifaemcdfb ...
- 第二天学习笔记:(MDN HTML学习、web安全策略与常见攻击、语义化)
一:Web入门 1:web文件命名 在文件名中应使用连字符(-).搜索引擎把连字符当作一个词的分隔符, 但不会以这种方式处理下划线. 养成在文件夹和文件名中使用小写,并且使用短横线而不是空格来分隔的习 ...
- 机器人中的轨迹规划(Trajectory Planning )
Figure. Several possible path shapes for a single joint 五次多项式曲线(quintic polynomial) $$\theta(t)=a_0+ ...
- H+ 添加(新增)Tab选项卡
//注:在contabs.js文件中 $(function () { }); 方法外 加入//注: data-name="' + menuName + '" 这句是加入的自定义属性 ...