FZU Fire Game

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
###
..#
#.#
题意:有两个人要放火,‘#’代表草地,‘.'代表空地,火不能通过空地进行扩散,现在有两个人要进行放火,可以在两个位置不同的地方放火,也可以在同一点进行放火,让我们求能否把整个地图的草地全部烧掉,如果能,就输出烧掉全部的草地所需花费的最小时间(火可以通过草地扩散,每一秒都会向上下左右四个方向进行扩散),如果不能就输出-1
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
#define inf 10000
int a[4]={1,-1,0,0};
int b[4]={0,0,1,-1};
char map[12][12];
int d[12][12],n,m,cnt;
struct node{
    int x,y,step;
};
int bfs(node t,node tt)//因为是两个着火点
{
    queue<node> Q;
    node v1,v2;
    memset(d,inf,sizeof(d));//把所有的位置全部复制为无穷大,然后寻找最小花费时间
    Q.push(t);
    d[t.x][t.y]=0;//把两个起始点燃烧时间变为0
    Q.push(tt);
    d[tt.x][tt.y]=0;
    while(!Q.empty())
    {
        v1=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            v2.x=v1.x+a[i];
            v2.y=v1.y+b[i];
            if(v2.x>=0&&v2.x<n&&v2.y>=0&&v2.y<m&&map[v2.x][v2.y]=='#'&&d[v2.x][v2.y]>d[v1.x][v1.y]+1)//如果满足条件且没有被火烧过
            {
                d[v2.x][v2.y]=d[v1.x][v1.y]+1;//燃烧时间肯定为扩散前的时间+1
                Q.push(v2);
            }
        }
    }
    int res=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(map[i][j]=='#')
            {
                res=max(res,d[i][j]);
            }
        }
    }
    return res;//如果有草地没有被烧掉,那么res肯定为inf
}
int main()
{
    int i,j,l,k=1,r,t,count;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        count=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='#')
                {
                    count++;
                }
            }
        }
        cout<<"Case "<<k++<<": ";
        if(count<=2)//如果草地的个数小于等于2,直接就输出0
        {
            cout<<"0"<<endl;
            continue;
        }
        cnt=inf;
        node x1,x2;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='#')
                {
                    for(l=0;l<n;l++)
                    {
                        for(r=0;r<m;r++)
                        {
                            if(l<i&&r<j) continue;
                            if(map[l][r]=='#')
                            {
                                x1.x=i,x1.y=j,x2.x=l,x2.y=r;
                                int ans=bfs(x1,x2);//寻找两个草地,ans是草地燃烧完的最小时间
                                cnt=min(cnt,ans);//如果有草地没有被烧掉,那么cnt肯定为inf
                            }
                        }
                    }
                }
            }
        }
        if(cnt==inf)//cnt为inf即有草地没有烧掉,那么肯定不能烧完整个图里的草地
        {
            cout<<"-1"<<endl;
        }else{
            cout<<cnt<<endl;//如果能烧完,输出最小花费时间
        }
    }
    return 0;
}

Sample Output

Case 1: 1Case 2: -1Case 3: 0Case 4: 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值