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.)c++

You can assume that the grass in the board would never burn out and the empty grid would never get fire.this

Note that the two grids they choose can be the same.spa

Input
The first line of the date is an integer T, which is the number of the text cases.code

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.blog

1 <= T <=100, 1 <= n <=10, 1 <= m <=10ci

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.get

Sample Input
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#input

Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2it

 

題意:ast

兩我的在一個平面草地上某兩個點放火,每一單位時間火就向四周四格蔓延(若是是草地的話),求問兩我的最少用多少時間能把整片草地燒完。

 

題解:

$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);
    }
}
相關文章
相關標籤/搜索