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

 

题意:

两个人在一个平面草地上某两个点放火,每一单位时间火就向四周四格蔓延(如果是草地的话),求问两个人最少用多少时间能把整片草地烧完。

 

题解:

$n,m$ 很小,可以暴力枚举两个人的起始放火点,然后BFS。对于每次BFS,返回把这片草地烧完所需的时间。

 

能过样例的代码(因为福大的OJ崩掉了……):

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};

int n,m;
char mp[15][15];

struct Node{
    int x,y;
    int step;
    Node(){}
    Node(int _x,int _y,int _step) {
        x=_x, y=_y, step=_step;
    }
    bool operator==(const Node &oth)const {
        return x==oth.x && y==oth.y && step==oth.step;
    }
};
int vis[15][15];
queue<Node> Q;
int bfs(Node a,Node b)
{
    memset(vis,-1,sizeof(vis));

    if(a==b) Q.push(a);
    else Q.push(a), Q.push(b);
    vis[a.x][a.y]=vis[b.x][b.y]=0;

    while(!Q.empty())
    {
        Node now=Q.front(); Q.pop();
        for(int k=0;k<4;k++)
        {
            Node nxt=Node(now.x+dx[k],now.y+dy[k],now.step+1);
            if(mp[nxt.x][nxt.y]=='.') continue;
            if(vis[nxt.x][nxt.y]!=-1) continue;
            vis[nxt.x][nxt.y]=nxt.step;
            Q.push(nxt);
        }
    }

    int res=-1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(mp[i][j]=='.') continue;
            if(vis[i][j]==-1) return INF;
            else res=max(res,vis[i][j]);
        }
    }
    return res;
}

int main()
{
    int T;
    cin>>T;
    for(int kase=1;kase<=T;kase++)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<15;i++) for(int j=0;j<15;j++) mp[i][j]='.';
        for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);

        int ans=INF;
        for(int i1=1;i1<=n;i1++)
        {
            for(int j1=1;j1<=m;j1++)
            {
                if(mp[i1][j1]=='.') continue;
                for(int i2=1;i2<=n;i2++)
                {
                    for(int j2=1;j2<=m;j2++)
                    {
                        if(mp[i2][j2]=='.') continue;
                        ans=min(ans,bfs(Node(i1,j1,0),Node(i2,j2,0)));
                    }
                }
            }
        }
        if(ans>=INF) ans=-1;
        printf("Case %d: %d\n",kase,ans);
    }
}

 

posted @ 2018-10-30 22:26  Dilthey  阅读(2350)  评论(0编辑  收藏  举报