【FZU 2150】Fire Game(BFS)

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

Sample Output

Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

题目大意

题目大概就是说,在两处地方同时点火(图中‘#’表示可以草地可以燃烧,‘.’表示不可燃烧),只在两处地方点一次,点火后,火会向上下左右蔓延。输出最少需要几秒可以把草地全部烧完,如果无法烧完所有草地,输出-1.

思路

这题主要是难在两个地方点火,也就是有两个开始的点,其实这并没有什么关系,只要在bfs开始时将两个起点都入队列就可以了。但是这题又要求最短的花费时间,可以枚举所以可以作为火源的任意两点,对于每两个火源都跑一遍bfs,求出一个最短的时间花费。

代码

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

using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=15;

int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int vis[maxn][maxn],n,m;
char map[maxn][maxn];


struct proc
{
    int x,y,step;
};

vector<proc> v;

int bfs(proc vw,proc vn)
{
    memset(vis,0,sizeof(vis));
    queue<proc> q;
    vw.step=0,vn.step=0;
    vis[vw.x][vw.y]=1,vis[vn.x][vn.y]=1;
    q.push(vw),q.push(vn);//将两个火源都入队列,顺序无关紧要
    int ans=INF;
    while(!q.empty())
    {
        vw=q.front();
        q.pop();
        ans=vw.step;
        for(int i=0;i<4;i++)
        {
            vn.x=vw.x+dir[i][0];
            vn.y=vw.y+dir[i][1];
            if(!vis[vn.x][vn.y]&&map[vn.x][vn.y]=='#'&&vn.x>=1&&vn.x<=n&&vn.y>=1&&vn.y<=m)
            {
                vis[vn.x][vn.y]=1;
                vn.step=vw.step+1;
                q.push(vn);
            }
        }
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        v.clear();
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            getchar();
            for(int j=1;j<=m;j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='#')
                {
                    v.push_back((proc){i,j,0});
                }
            }
        }
        int ans=INF;
        for(int i=0;i<v.size();i++)
        {
            for(int j=i;j<v.size();j++)
            {
                int tmp=bfs(v[i],v[j]);//枚举任意两个火源
                bool flag=true;
                for(int k=1;k<=n;k++)
                {
                    for(int z=1;z<=m;z++)
                    {
                        if(!vis[k][z]&&map[k][z]=='#')//判断当前火源能否烧完全部草地,如果存在一个vis[i][j]为0,表示这块草地没被燃烧。
                        {
                            flag=false;
                            break;
                        }
                    }
                    if(!flag) break;
                }
                if(flag)
                {
                    ans=min(ans,tmp);//求最小的一个时间花费
                }
            }
        }
        printf("Case %d: ",cas);
        if(ans==INF)
        {
            printf("-1\n");
        }
        else printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/iceiceicpc/article/details/52097422