FZU 2150 Fire Game

Problem 2150 Fire Game

Accept: 2556    Submit: 8845
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

43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#

 Sample Output

Case 1: 1Case 2: -1Case 3: 0Case 4: 2
题意:
有俩小伙,闲着没事,要玩火,玩就玩吧,还要烧人家的草坪。(可怕)‘#“是草,’.‘是空地,他俩开始一人点一堆,火每分钟可以往四个方向延展,问这俩小伙需要等多长时间能把人家的草坪烧光(熊孩子,烧就烧吧,还要最短时间烧完)。空地点不着,草是烧不光的。
解析:
数据量不是很大,最大是10x10的方格,暴力遍历每两个草堆,DFS输出最短书剑即可。
AC代码:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <climits>
using namespace std;
const int MAXN=11;
char data[MAXN][MAXN];///字符记录数组
bool pin[MAXN][MAXN];///DFS标记数组
int tim[MAXN][MAXN];///火烧到时间记录数组
int n, m;
int Move[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};///移动
struct node
{
    int numx, numy, step;
};
bool zhao(int a, int b)///判断是否出界
{
    if(a>=0&&a<n&&b>=0&&b<m)
        return true;
    else
        return false;
}
void BFS(int a, int b, int c, int d)
{
    struct node A, B;
    queue<struct node>Q;
    A=(node)
    {
        a,b,0
    };
    Q.push(A);
    if(a!=b||c!=d)
    {
        B=(node)
        {
            c,d,0
        };
        Q.push(B);
    }
    while(!Q.empty())
    {
        A=Q.front();
        Q.pop();
        pin[A.numx][A.numy]=true;
        tim[A.numx][A.numy]=min(tim[A.numx][A.numy],A.step);
        for(int i=0; i<4; i++)
        {
            int c=A.numx+Move[i][0];
            int d=A.numy+Move[i][1];
            if(zhao(c,d)&&!pin[c][d]&&data[c][d]!='.')
            {
                B=(node)
                {
                    c,d,A.step+1
                };
                Q.push(B);
                pin[c][d]=true;
            }
        }
    }
}
int judge()///遍历数组,求得烧完的时间
{
    int a=INT_MIN;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(data[i][j]=='#')
                a=max(tim[i][j],a);
        }
    }
    return a;
}
void start()///初始化
{
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
            tim[i][j]=INT_MAX;
    }
}
int main()
{
    int T;
    cin>>T;
    for(int t=1; t<=T; t++)
    {
        cin>>n>>m;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>data[i][j];
                if(data[i][j]=='#')
                    pin[i][j]=true;
                else
                    pin[i][j]=false;
            }
        }
        int a, b, c, d;
        int minn=INT_MAX;
        for(a=0; a<n; a++)
        {
            for(b=0; b<m; b++)
            {
                if(data[a][b]!='#')
                    continue;///剪枝
                for(c=0; c<n; c++)
                {
                    for(d=0; d<m; d++)
                    {
                        if(data[c][d]!='#')
                            continue;///剪枝
                        start();
                        memset(pin,false,sizeof(pin));
                        BFS(a,b,c,d);
                        minn=min(minn,judge());
                    }
                }
            }
        }
        if(minn==INT_MAX)
            minn=-1;
        cout<<"Case "<<t<<": "<<minn<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37495823/article/details/76654828