Fire Game(BFS的应用)

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

扫描二维码关注公众号,回复: 3478035 查看本文章
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

题目大意

一块地上面有草和空地,有两个人想要把草烧光,这样他俩就可以开心的OOXX,他俩都要在一块地上放火(每人只能放一次),可以在相同或者不同的位置放,火可以向四周蔓延,蔓延一次话费1分钟,问他俩把这块地上草烧完所用到的最小时间。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;
const int MAX = 0x3f3f3f3f;
char mapp[13][13];
int visit[13][13];
int n, m;
int next1[4] = {1, -1, 0, 0};
int next2[4] = {0, 0, 1, -1};
struct node
{
    int x;
    int y;
    int step;
} a[110], f, t;

int BFS(node s, node d)
{
    queue <node> q;
    int i;
    int ans;
    visit[s.x][s.y] = 1;
    visit[d.x][d.y] = 1;
    q.push(s);
    q.push(d);
    while(!q.empty())
    {
        f = q.front();
        ans = f.step;
        q.pop();
        for(i=0; i<4; i++)
        {
            int dx = f.x + next1[i];
            int dy = f.y + next2[i];
            if(dx>=0&&dy>=0&&dx<n&&dy<m&&visit[dx][dy]==0&&mapp[dx][dy]=='#')
            {
                visit[dx][dy] = 1;
                t.x = dx;
                t.y = dy;
                t.step = f.step+1;
                q.push(t);
            }
        }
    }
    return ans;
}

bool xin()//检查草是否全烧完
{
    int i, j;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            if(visit[i][j]==0 && mapp[i][j]=='#')
                return 0;
        }
    }
    return 1;
}

int main()
{
    int T, top, ans, num, i, j, u;
    scanf("%d", &T);
    u = 1;
    while(T--)
    {

        scanf("%d%d", &n, &m);
        top = 0;
        ans = MAX;
        for(i=0; i<n; i++)
        {
            scanf("%s", mapp[i]);
            for(j=0; j<m; j++)
            {
                if(mapp[i][j]=='#')
                {
                    f.x = i;
                    f.y = j;
                    f.step = 0;
                    a[top++] = f;
                }
            }
        }
        for(i=0; i<top; i++)
        {
            for(j=0; j<top; j++)
            {
                memset(visit, 0, sizeof(visit));
                num = BFS(a[i], a[j]);
                if(num<ans && xin())
                    ans = num;
            }
        }
        if(ans==MAX)
            printf("Case %d: -1\n", u++);
        else
            printf("Case %d: %d\n", u++, ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42137874/article/details/82192976