Fire Game
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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
草原点火的游戏,随机点燃两个地方,求是否能燃完所有草堆,如果能,求燃完所需要的最短时间。
先枚举起手点火的两个地方(可以重叠),然后暴力模拟便可,最后比较时间。
//
// main.cpp
// 简单搜索-I-Fire Game
//
// Created by 袁子涵 on 15/8/3.
// Copyright (c) 2015年 袁子涵. All rights reserved.
//
#include <iostream>
#include <string.h>
#include <stdio.h>
#define minof(a,b) ((a)>(b)?(b):(a))
using namespace std;
int T;
int maze[101][101];
int maze_1[101][101];
int n,m;
int times;
int flag=-1;
typedef struct plaz
{
int x,y;
}Plaz;
Plaz origin_1,origin_2;
void move(int x,int y)
{
if (x>0) {
if (maze[x-1][y]==1) {
maze[x-1][y]=3;
}
}
if (x<n-1) {
if (maze[x+1][y]==1) {
maze[x+1][y]=3;
}
}
if (y>0) {
if (maze[x][y-1]==1) {
maze[x][y-1]=3;
}
}
if (y<m-1) {
if (maze[x][y+1]==1) {
maze[x][y+1]=3;
}
}
}
void Fire()
{
maze[origin_1.x][origin_1.y]=2;
maze[origin_2.x][origin_2.y]=2;
times=0;
while (1) {
//点燃的向周围延伸
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (maze[i][j]==2) {
maze[i][j]=0;
move(i,j);
}
}
}
int f=1;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (maze[i][j]==1 || maze[i][j]==3) {
f=0;
break;
}
}
}
if (f) {
if (flag==-1) {
flag=times;
return;
}
flag=minof(times,flag);
return;
}
f=1;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (maze[i][j]==3) {
f=0;
maze[i][j]=2;
}
}
}
if (f) {
return;
}
times++;
}
}
int main(int argc, const char * argv[]) {
cin >> T;
int c=T;
char x;
while (c--) {
memset(maze, 0, sizeof(maze));
memset(maze_1, 0, sizeof(maze_1));
flag=-1;
cin >> n >> m;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
cin >> x;
switch (x) {
case '#':
maze[i][j]=1;
break;
case '.':
maze[i][j]=0;
break;
default:
break;
}
}
}
memcpy(maze_1, maze, sizeof(maze));
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
origin_1.x=i;
origin_1.y=j;
if (maze[origin_1.x][origin_1.y]!=1) {
continue;
}
for (int p=i; p<n; p++) {
if (p==origin_1.x) {
for (int q=origin_1.y; q<m; q++) {
origin_2.x=p;
origin_2.y=q;
if (maze[origin_2.x][origin_2.y]!=1) {
continue;
}
Fire();
memcpy(maze, maze_1, sizeof(maze));
}
}
else
{
for (int q=0; q<m; q++) {
origin_2.x=p;
origin_2.y=q;
if (maze[origin_2.x][origin_2.y]!=1) {
continue;
}
Fire();
memcpy(maze, maze_1, sizeof(maze));
}
}
}
}
}
cout << "Case " << T-c << ": " << flag << endl;
}
return 0;
}