FZU 2150 Fire Game (队列压入两个点)

来源:互联网 发布:淘宝购物并发技术 编辑:程序博客网 时间:2024/06/05 20:47

http://acm.fzu.edu.cn/problem.php?pid=2150

Problem 2150 Fire Game

Accept: 1160    Submit: 4121
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
给出一个m*n的图,‘#’表示草坪,‘ . ’表示空地,然后可以选择在任意的两个草坪格子点火,火每 1 s会向周围四个格子扩散,问选择那两个点使得燃烧所有的草坪花费时间最小?

定义一个结构体数组保存所有的草坪,枚举法同时将两个点进入队列得到一个较大的时间,然后求出枚举的所有的情况的最短时间,当没有‘#’和只有一个‘#’时特殊处理一下就Ok了

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <stack>#include <queue>using namespace std;#define N 106#define met(a, b) memset (a, b, sizeof (a))typedef long long LL;#define INF 0x3f3f3f3fconst int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};struct node{    int x, y, step;}s1, s2, cnt[N];int vis[N][N], n, m, k;char g[N][N];int BFS (){    queue <node> que;    que.push (s1);//将枚举的两个点进入队列    que.push (s2);//将枚举的两个点进入队列    met (vis, 0);    int maxn = 0;    vis[s1.x][s1.y] = 1;    vis[s2.x][s2.y] = 1;    while (que.size())    {        node q = que.front(); que.pop();        int flag = 0;        for (int i=0; i<k; i++)            if (!vis[cnt[i].x][cnt[i].y])            {//判断是否还有草坪可烧                flag = 1;                break;            }        if (!flag) return maxn;//没有草坪可烧就返回较大的那个时间        for (int i=0; i<4; i++)        {            node p = q;            p.x += dir[i][0], p.y += dir[i][1];            if (p.x>=0 && p.x<n && p.y>=0 && p.y<m && !vis[p.x][p.y] && g[p.x][p.y]=='#')            {                vis[p.x][p.y] = 1;                p.step++;                que.push (p);                maxn = max (maxn, p.step);//舍弃较短的那个时间            }        }    }    return -1;}int main (){    int t, nCase = 1;    scanf ("%d", &t);    while (t--)    {        scanf ("%d %d", &n, &m);        met (g, 0);        int minx = INF;        k = 0;        for (int i=0; i<n; i++)        {            getchar ();            for (int j=0; j<m; j++)            {                scanf ("%c", &g[i][j]);                if (g[i][j] == '#')//将所有的草坪都保存在一个结构体数组里                    cnt[k++] = (node){i, j, 0};            }        }        if (!k || k==1)        {            printf ("Case %d: %d\n", nCase++, 0);            continue;        }        int flag = 0;        for (int i=0; i<k-1; i++)        {            for (int j=i+1; j<k; j++)            {                s1 = cnt[i], s2 = cnt[j];                int ans = BFS ();                if (ans != -1)                {                    minx = min (minx, ans);//求得最短的时间                    flag = 1;                }            }        }        if (!flag) minx = -1;        printf("Case %d: %d\n", nCase++, minx);    }    return 0;}


1 0