FZU - 2150 Fire Game(15.10.10 搜索专题)bfs

来源:互联网 发布:松江消防主机3208编程 编辑:程序博客网 时间:2024/04/30 01:19
Fire Game
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

Submit Status

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: 1Case 2: -1Case 3: 0Case 4: 2


思路:两个熊孩子在n*m的平地上放火玩,#表示草,两个熊孩子分别选一个#格子点火,火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一。求烧完所有的草需要的最少时间。如不能烧完输出-1。

依次枚举两个#格子,BFS出需要的时间,取最小的一个

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define inf 0x3f3f3fconst int dirx[4] = { 0, 0, 1, -1 };const int diry[4] = { 1, -1, 0, 0 };char map[15][15];int dis[15][15];int n, m;struct Point{int x, y;Point(int xx, int yy) :x(xx), y(yy){}Point(){}};queue<Point>que;int bfs(int x1, int y1, int x2, int y2){memset(dis, inf, sizeof(dis));que.push(Point(x1, y1));que.push(Point(x2, y2));dis[x1][y1] = 0;dis[x2][y2] = 0;while (!que.empty()){Point loc = que.front();que.pop();for (int i = 0; i < 4; i++){int xx = loc.x + dirx[i];int yy = loc.y + diry[i];if (xx >= 0 && xx < n && yy >= 0 && yy<m && map[xx][yy] == '#' && dis[xx][yy] > dis[loc.x][loc.y] + 1){dis[xx][yy] = dis[loc.x][loc.y] + 1;que.push(Point(xx, yy));}}}int ret = 0;for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)if (map[i][j] == '#'){ret = max(ret, dis[i][j]);}return ret;}int main(){int casen;cin >> casen;for (int cas = 1; cas <= casen; cas++){while (!que.empty())que.pop();cin >> n >> m;for (int i = 0; i < n; i++)cin >> map[i];int ans = inf;for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)if (map[i][j] == '#')for (int ii = 0; ii < n; ii++)for (int jj = 0; jj < m; jj++)if (map[ii][jj] == '#'){int temp = bfs(i, j, ii, jj);ans = min(ans, temp);}if (ans == inf) ans = -1;cout << "Case " << cas << ": " << ans << endl;}}

0 0
原创粉丝点击