FZU 2150 Fire Game(双向BFS好题)

Problem 2150 Fire Game

Accept: 3410    Submit: 11684
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

4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#

Sample Output

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

题目大意:给你一片草地,你可以在两个地方放火,求出把所有的草都烧完的最短时间,火每秒钟可以向上下左右四个方向蔓延,如果烧不完就输出“-1”。

解题思路:

先求出一共有几块草(连通的)。

如果大于2就直接输出“-1”。

如果等于2就分别求出两块烧完的最小值,输出大的那个。

如果等于1就枚举两个点进行双向BFS,取最小时间。

如果等于0就输出0呗。

AC代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, a, b) G[a].push_back(b);
using namespace std;
typedef long long LL;
typedef pair<LL, pair<int, LL> > LLppar;
typedef pair<int, int> par;
typedef pair<LL, int> LLpar;
const int mod = 998244353;
const int INF = 1e9+7;
const int N = 1010;
const double pi = 3.1415926;

int m, n;
int vis[20][20];
char str[20][20];
int dx[8] = {0, 1, 0, -1};
int dy[8] = {1, 0, -1, 0};

struct node
{
    int x;
    int y;
    int len;
};

vector<node> v[3]; //用vector来记录每一块的点

bool check(int x, int y)
{
    if(x < 0 || x >= n || y < 0 || y >= m) return false;
    if(vis[x][y] || str[x][y] == '.') return false;
    return true;
}

int bfs(int x, int y, int a, int b, int k, int flag) //flag==1的话,进行双向BFS
{
    queue<node> q;
    node st;
    st.x = x;
    st.y = y;
    st.len = 0;
    if(flag) {
        node st2;
        st2.x = a;
        st2.y = b;
        st2.len = 0;
        vis[a][b] = 1;
        q.push(st2);
    }
    vis[x][y] = 1;
    q.push(st);
    v[k].push_back(st); //记录每一块的点
    int sum = 0;
    while(!q.empty()) {
        node cur = q.front();
        q.pop();
        sum = cur.len;
        for(int i = 0; i < 4; i ++) {
            int ix = cur.x + dx[i];
            int iy = cur.y + dy[i];
            if(check(ix, iy)) {
                vis[ix][iy] = 1;
                node next;
                next.x = ix;
                next.y = iy;
                next.len = cur.len + 1;
                q.push(next);
                v[k].push_back(next);
            }
        }
    }
    return sum;
}

int main()
{
    int T, sx, sy, t;
    scanf("%d", &T);
    t = 1;
    while(T --) {
        for(int i = 0; i < 3; i ++) v[i].clear();
        mem0(vis);
        scanf("%d%d", &n, &m);
        int k = 0, sum = 0, ans = 0;
        for(int i = 0; i < n; i ++)
            scanf("%s", str[i]);
        for(int i = 0; i < n; i ++) {
            for(int j = 0; j < m; j ++) {
                if(check(i, j)) {
                    bfs(i, j, i, j, k ++, 0);
                }
                if(k > 2) break;
            }
        }
        if(k > 2) printf("Case %d: -1\n", t ++);
        else {
            ans = INF;
            if(k == 1) { //只有一块,双向BFS
                for(int j = 0; j < v[0].size(); j ++) { //枚举点
                    for(int s = j; s < v[0].size(); s ++) {
                        mem0(vis);
                        ans = min(ans, bfs(v[0][j].x, v[0][j].y, v[0][s].x, v[0][s].y, 2, 1));
                    }
                }
                sum = ans;
            }else
            for(int i = 0; i < k; i ++) { //枚举块
                ans = INF;
                for(int j = 0; j < v[i].size(); j ++){ //枚举点
                    mem0(vis);
                    ans = min(bfs(v[i][j].x, v[i][j].y, 0, 0, 2, 0), ans);
                }
                sum = max(ans, sum); //取大的
            }
            printf("Case %d: %d\n", t ++, sum); //只有一块的话会直接输出0
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/I_believe_CWJ/article/details/80967491