Problem 2150 Fire Game

Accept: 3417    Submit: 11710
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

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

   Sample Output

Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

               简单的说就是找到两个#位置的地方(草坪),然后点燃,每一秒后可以向四个基础方向扩展,但是遇到"."(石头?)就会停止扩散,问能否把所有的草坪都烧光,不能的话输出-1,否则输出,可以的最短时间。

                对于第一个问题,能否全部烧到,就是看有多少连通区域,没遇到没经历过地方用DFS查一遍,看看有多少联通块就好了。

               对于第二个问题,因为数据范围挺小的(10*10),所以我们就可以直接暴力一边,每个情况都查找一下,看每种情况下的最长时间是多少,然后到最后再看看这些时间里面用时最短的是多少就好了;

#include<iostream>
#include<cstdio>
#include<queue>
#include<utility>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
int vis[20][20];
char map[20][20];
int fx[4][2] = { 1,0,0,1,-1,0,0,-1 };
int m, n;
int BFS(int a, int b, int c, int d)
{
	memset(vis, 0, sizeof(vis));
	queue<pair<int, int>>q;
	q.push(make_pair(a, b));
	q.push(make_pair(c, d));
	vis[a][b] = 1;
	vis[c][d] = 1;
	int maxn = 0;
	while (!q.empty())
	{
		pair<int, int>top = q.front();
		q.pop();
		maxn = max(maxn, vis[top.first][top.second]);
		for (int s = 0; s < 4; s++)
		{
			int r = top.first + fx[s][0];
			int e = top.second + fx[s][1];
			if (r >= 0 && r < m&&e >= 0 && e < n && vis[r][e] == 0 && map[r][e] == '#')
			{
				vis[r][e] = vis[top.first][top.second] + 1;
				q.push(make_pair(r, e));
			}
		}
	}
	for (int s = 0; s < m; s++)
	{
		for (int w = 0; w < n; w++)
		{
			if (vis[s][w] == 0 && map[s][w] == '#')
			{
				return inf;
			}
		}
	}
	return maxn-1;
}
void DFS(int x, int y)
{
//	cout << x << " " << y << endl;
	for (int s = 0; s < 4; s++)
	{
		int ri = x + fx[s][0];
		int li = y + fx[s][1];
		if (ri >= 0 && ri < m && li >= 0 && li < n && vis[ri][li]==0 && map[ri][li]=='#')
		{
			vis[ri][li] = 1;
			DFS(ri, li);
		}
	}
}
int main()
{
	int te;
	scanf("%d", &te);
	int cas = 1;
	while (te--)
	{
		ios::sync_with_stdio(0);
		int ans = inf;
		memset(vis, 0, sizeof(vis));
		cin >> m >> n;
		for (int s = 0; s < m; s++)
		{
			for (int w = 0; w < n; w++)
			{
				cin >> map[s][w];
			}
		}
		int sum = 0;
		for (int s = 0; s < m; s++)
		{
			for (int w = 0; w < n; w++)
			{
				if (map[s][w] == '#' && vis[s][w] == 0)
				{
					DFS(s, w);
					sum++;
				}
			}
		}
		if (sum > 2)
		{
			printf("Case %d: -1\n", cas++);
			continue;
		}
		for (int x1 = 0; x1 < m; x1++)
		{
			for (int y1 = 0; y1 < n; y1++)
			{
				for (int x2 = 0; x2 < m; x2++)
				{
					for (int y2 = 0; y2 < n; y2++)
					{
						if (map[x1][y1] == '#'&&map[x2][y2] == '#')
							ans = min(ans, BFS(x1, y1, x2, y2));
					}
				}
			}
		}
		printf("Case %d: %d\n", cas++, ans);
	}
	return 0;
}