您的位置:首页 > 其它

FZU 2150 Fire Game

2015-08-17 10:17 267 查看

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

43 3

.#.

###

.#.

3 3

.#.

#.#

.#.

3 3

...

#.#

...

3 3

###

..#

#.#

Sample Output

Case 1: 1

Case 2: -1

Case 3: 0

Case 4: 2

题目大意是选取两丛草,然后开始烧并开始蔓延,问最短多少时间可以烧完
考虑用BFS,因为数据给的实在是比较小,所以就当是基础的BFS来练了,暴力枚举所有的样例,然后BFS算出最短的时间,比较保存最短的时间
/*************************************************************************
> File Name: Fire_Game.cpp
> Author: Zhanghaoran0
> Mail: chiluamnxi@gmail.com
> Created Time: 2015年08月02日 星期日 08时53分45秒
************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int T;
char a[15][15];
int n, m;
int num = 0;
bool flag[15][15];
int grass[201][2];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
struct node{
int x;
int y;
int step;
}q[100000];

int BFS(int xx, int yy){
memset(flag, false, sizeof(flag));
flag[grass[xx][0]][grass[xx][1]] = flag[grass[yy][0]][grass[yy][1]] = 1;
int head = 0;
int tail = 1;
q[head].x = grass[xx][0];
q[head].y = grass[xx][1];
q[head].step = 0;
q[tail].x = grass[yy][0];
q[tail].y = grass[yy][1];
q[tail].step = 0;
tail ++;
int ans = 1000001;
int cas = 1;
while(head < tail){
ans = q[head].step;
for(int i = 0; i < 4; i ++){
q[tail].x = q[head].x + dx[i];
q[tail].y = q[head].y + dy[i];
q[tail].step = q[head].step + 1;

if(q[tail].x > 0 && q[tail].y > 0 && q[tail].x <= n && q[tail].y <= m && flag[q[tail].x][q[tail].y] == false && a[q[tail].x][q[tail].y] == '#'){
flag[q[tail].x][q[tail].y] = 1;
tail ++;
}
}
head ++;
}
return ans;
}

int main(void){
int T;
cin >> T;
for(int cas = 1; cas <= T; cas ++){
int S = 0;
cin >> n >> m;
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
cin >> a[i][j];
if(a[i][j] == '#'){
grass[S][0] = i;
grass[S ++][1] = j;
}
}
}
int ans = 1000001;
for(int i = 0; i < S; i ++){
for(int j = 0; j < S; j ++){
int temp = BFS(i, j);
bool suc = false;
for(int k = 1; k <= n; k ++){
for(int l = 1; l <= m; l ++){
if(flag[k][l] == 0 && a[k][l] == '#'){
suc = true;
break;
}
}
if(suc == true)
break;
}
if(!suc)
ans = min(ans, temp);
}
}
printf("Case %d: ", cas);
if(ans == 1000001)
printf("-1\n");
else
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: