• Fire Game FZU 2150


    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

    思路

    两点bfs,两个人烧到的草地有重复的情况下,取较小的值,然后把这个点再放到队列里去更新别的点即可。

    #include<iostream>
    #include<queue>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    
    using namespace std;
    
    const int N = 20;
    
    #define PII pair<int, int>
    
    int n, m;
    int t;
    
    char g[N][N];
    int w[N][N], vis[N][N];
    int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
    
    queue<PII> q;
    
    int bfs(int cnt){
        int res = 2;
        while(q.size()){
            PII t = q.front();
            q.pop();
            int x = t.first, y = t.second;
            
            for(int i = 0; i < 4; i ++){
                int a = x + dx[i], b = y + dy[i];
                if(a < 0 || a >= n || b < 0 || b >= m || g[a][b] == '.') continue;
                if(vis[a][b]){
                    if(w[x][y] + 1 < w[a][b]){
                        w[a][b] = w[x][y] + 1;
                        q.push(make_pair(a, b));
                    }
                    continue;
                }
                vis[a][b] = 1;
                res ++;
                w[a][b] = w[x][y] + 1;
                q.push(make_pair(a, b));
            }
        }
        
        if(res < cnt) return 0x3f3f3f3f;
        
        int mx = 0;
        
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < m; j ++)
                mx = max(mx, w[i][j]);
        
        return mx;
    }
    
    int main(){
        cin >> t;
        
        for(int i = 1; i <= t; i ++){
            vector<PII> pts;
            
            cin >> n >> m;
            
            int cnt = 0;
            
            for(int i = 0; i < n; i ++)
                for(int j = 0; j < m; j ++){
                    cin >> g[i][j];
                    if(g[i][j] == '#')
                        pts.push_back(make_pair(i, j));
                }
            int res = 0x3f3f3f3f;
            for(int i = 0; i < pts.size(); i ++)
                for(int j = 0; j < pts.size(); j ++){
                    memset(vis, 0, sizeof vis);
                    memset(w, 0, sizeof w);
                    q.push(pts[i]);
                    q.push(pts[j]);
                    
                    vis[pts[i].first][pts[i].second] =
                    vis[pts[j].first][pts[j].second] = 1;
                    
                    res = min(res, bfs(pts.size()));
                }
            
            printf("Case %d: %d\n", i, res == 0x3f3f3f3f ? -1 : res);
        }
        
        return 0;
    }
    
  • 相关阅读:
    Range对象
    WorkSheet属性和方法
    Workbooks对象集
    常用对象之Application
    过程(Sub和Function)
    VBA基本语言结构
    Python网络编程-UDP(基于socket)
    Python网络编程-TCP(基于socket)
    二叉树的创建与遍历算法-C语言
    级数收敛的判定步骤
  • 原文地址:https://www.cnblogs.com/tomori/p/13433437.html
Copyright © 2020-2023  润新知