链接: 原题网址.
题意
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
大概意思
两个人在一堆草丛里放火,火会点燃旁边的草,空地能够阻隔火焰,没人放一把火,问能不能把所有草烧掉,能烧掉的话最少要多久时间。
题解
主要是看数据大小很小,可以通过暴力搜索每一个点,直接求最少时间。和一般的题目唯一的区别就是要把每一个草丛的位置记录下来,双起点BFS。一开始我的想法是通过BFS处理出每一堆草丛的位置,再进行BFS处理找草丛放火时间的最小值,但是没有考虑只有一堆草丛的时候两个人在同一堆草丛里放火,这个时间就比一个起点的要快,所以虽然再修改之后我一开始的代码也能AC了,但是没有双起点的代码来的简单清爽。
代码
#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
#define speed {ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
const int maxn = 11;
const double PI = 3.14;
int n, m, dis[maxn][maxn], len, maxlen;
char a[maxn][maxn];
struct node
{
int x, y, step;
};
struct Node
{
int x, y;
}ma[110];
int xmove[4][2] = { {1,0},{-1,0},{0,1},{0,-1} }; //向四个方向搜索
int ok(int x, int y)//检查函数
{
if (x > 0 && y > 0 && x <= m && y <= n && a[x][y] == '#' && dis[x][y] == 0)//超出边界或者不可走
return 1;
return 0;
}
void bfs(Node a, Node b)//广搜
{
queue<node>q;//有人说用优先队列更节约空间
while (!q.empty())q.pop();
node aa, aaa, bb;
memset(dis, 0, sizeof dis);
aa.x = a.x;
aa.y = a.y;
aa.step = 0;
aaa.x = b.x;
aaa.y = b.y;
aaa.step = 0;
dis[aa.x][aa.y] = 1;
dis[aaa.x][aaa.y] = 1;
q.push(aa);
q.push(aaa);
//就这里从一个起点变成两个,其他没差
while (!q.empty())
{
bb = q.front();
len = max(len, bb.step);
q.pop();
for (int i = 0; i < 4; i++)
{
aa.x = bb.x + xmove[i][0];
aa.y = bb.y + xmove[i][1];
if (ok(aa.x, aa.y))
{
aa.step = bb.step + 1;
dis[aa.x][aa.y] = 1;
q.push(aa);
}
}
}
}
int main() {
int t, cnt = 0;
cin >> t;
for (int w = 1; w <= t; w++) {
scanf("%d %d", &m, &n); cnt = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
if (a[i][j] == '#') {
ma[cnt].x = i;
ma[cnt].y = j;
cnt++;//记录每一处草丛的位置
}
}
}
maxlen = inf;//预设烧不完所有草丛
for (int i = 0; i < cnt; i++) {
for (int j = i + 1; j < cnt; j++) {
len = -1;//时间
bfs(ma[i], ma[j]);
int flag = 0;
for (int k = 1; k <= m; k++)
{
for (int p = 1; p <= n; p++)
{
if (a[k][p] == '#' && dis[k][p] == 0)//遍历有没有草丛没烧完
{
flag = 1;
break;
}
}
if (flag == 1)
break;
}
if (flag == 0)//草丛全部烧完的情况下去最快的时间
maxlen = min(maxlen, len);
}
}
if (cnt <= 2)cout << "Case " << w << ": " << "0" << endl;//只有两处草丛的话开始全点了就行,不用扩散了
else if (maxlen == inf)cout << "Case " << w << ": " << "-1" << endl;
else cout << "Case " << w << ": " << maxlen << endl;
}
return 0;
}
//一点样例
//100
//5 5
//#####
//#####
//#####
//#####
//#####
//5 5
//#####
//#...#
//#...#
//#...#
//#####
//10 10
//#####.....
//#####.....
//#####.....
//#####.....
//#####.....
//.....#####
//.....#...#
//.....#...#
//.....#...#
//.....#####
//3 3
//.#.
//###
//.#.
//3 3
//.#.
//#.#
//.#.
//3 3
//...
//#.#
//...
//3 3
//###
//..#
//#.#