FZU 2150 Fire Game (BFS)

题目链接:

FZU 2150 Fire Game (BFS)

题目大意:

n* m 场地,#表示草坪 ,每次选两个草坪,可以向周围四块草丛扩展,扩散时间为1秒,求最少时间,扩散整个草坪.

思路:

穷举:两个点组合,之后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 Output

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

/************************************************************************* > File Name: fzu_2150_t.cpp > Author: dulun > Mail: [email protected] > Created Time: 2016年04月13日 星期三 20时44分39秒 ************************************************************************/

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<algorithm>
#define LL long long
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 50086;
struct Node
{
    int x, y, cnt;
};
int m, n;
vector<Node> v;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char g[15][15];
bool vis[15][15];

int bfs(Node a, Node b)
{
    memset(vis, 0, sizeof(vis));
    queue<Node> q;
    vis[a.x][a.y] = vis[b.x][b.y] = true;
    int ans = inf;
    q.push(a);
    q.push(b);

    while(!q.empty())
    {
        a = q.front();
        q.pop();
        ans = a.cnt;
        for(int i = 0; i < 4; i++)
        {
            b.x = a.x + dx[i];
            b.y = a.y + dy[i];
            b.cnt = a.cnt+1;
            if(!vis[b.x][b.y] && g[b.x][b.y] == '#' && b.x>0 && b.y>0 && b.x<=n && b.y<=m )
            {
                vis[b.x][b.y] = true;
                q.push(b);
            }
        }
    }
    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", &g[i][j]);
                if(g[i][j]  == '#')
                {
                    v.push_back((Node){i, j, 0});
                }
            }
        }
        int ans = inf;
        for(int i = 0; i < v.size(); i++)
        {
            for(int j = 0; j < v.size(); j++)
            {
                int tmp = bfs(v[i], v[j]);
                bool ok = true;

                for(int k = 1; k <= n; k++)
                {
                    for(int f = 1; f <= m; f++)
                    {
                        if(!vis[k][f] && g[k][f] == '#')
                        {
                            ok = false;
                            break;
                        }
                    }
                    if(ok == false) break;
                }
                if(ok) ans = min(ans, tmp);
            }
        }
        printf("Case %d: ", cas);
        if(ans == inf) printf("-1\n");
        else           printf("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(扩展,bfs)