zoukankan      html  css  js  c++  java
  • FZU 2150 Fire Game

     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

    43 3
    .#.
    ###
    .#.
    3 3
    .#.
    #.#
    .#.
    3 3
    ...
    #.#
    ...
    3 3
    ###
    ..#
    #.#

    Sample Output

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



    题目大意是选取两丛草,然后开始烧并开始蔓延,问最短多少时间可以烧完
    考虑用BFS,因为数据给的实在是比较小,所以就当是基础的BFS来练了,暴力枚举所有的样例,然后BFS算出最短的时间,比较保存最短的时间
    /*************************************************************************
    	> File Name: Fire_Game.cpp
    	> Author: Zhanghaoran0
    	> Mail: chiluamnxi@gmail.com
    	> Created Time: 2015年08月02日 星期日 08时53分45秒
     ************************************************************************/
    
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    int T;
    char a[15][15];
    int n, m;
    int num = 0;
    bool flag[15][15];
    int grass[201][2];
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    struct node{
        int x;
        int y;
        int step;
    }q[100000];
    
    
    int BFS(int xx, int yy){
        memset(flag, false, sizeof(flag));
        flag[grass[xx][0]][grass[xx][1]] = flag[grass[yy][0]][grass[yy][1]] = 1;
        int head = 0;
        int tail = 1;
        q[head].x = grass[xx][0];
        q[head].y = grass[xx][1];
        q[head].step = 0;
        q[tail].x = grass[yy][0];
        q[tail].y = grass[yy][1];
        q[tail].step = 0;
        tail ++;
        int ans = 1000001;
        int cas = 1;
        while(head < tail){
            ans = q[head].step;
            for(int i = 0; i < 4; i ++){
                q[tail].x = q[head].x + dx[i];
                q[tail].y = q[head].y + dy[i];
                q[tail].step = q[head].step + 1;
    
                if(q[tail].x > 0 && q[tail].y > 0 && q[tail].x <= n && q[tail].y <= m && flag[q[tail].x][q[tail].y] == false && a[q[tail].x][q[tail].y] == '#'){
                    flag[q[tail].x][q[tail].y] = 1;
                    tail ++;
                }
            }
            head ++;
        }
        return ans;
    }
    
    int main(void){
        int T;
        cin >> T;
        for(int cas = 1; cas <= T; cas ++){
            int S = 0;
            cin >> n >> m;
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= m; j ++){
                    cin >> a[i][j];
                    if(a[i][j] == '#'){
                        grass[S][0] = i;
                        grass[S ++][1] = j;
                    }
                }
            }
            int ans = 1000001;
            for(int i = 0; i < S; i ++){
                for(int j = 0; j < S; j ++){
                    int temp = BFS(i, j);
                    bool suc = false;
                    for(int k = 1; k <= n; k ++){
                        for(int l = 1; l <= m; l ++){
                            if(flag[k][l] == 0 && a[k][l] == '#'){
                                suc = true;
                                break;
                            }
                        }
                        if(suc == true)
                            break;
                    }
                    if(!suc)
                        ans = min(ans, temp);
                }
            }
            printf("Case %d: ", cas);
            if(ans == 1000001)
                printf("-1
    ");
            else 
                printf("%d
    ", ans);
        }
        return 0;
    }
    
    


  • 相关阅读:
    我用纯C语言开发的中英文混合分词服务器3.0正式发布,词库190多万词,每秒切分5万+,同时提供 c、java、C#、delphi、js调用范例
    藏拙空间上线了!
    说实话我只能灌水,我谈技术你们有几个懂的啊?不信?随便发一段我写的代你们有几个能看懂的啊?
    明明三句话就能说清楚的事,专家们长篇大论,为何?
    正在开发云ERP,业务功能与天心CS ERP一模一样, 欢迎大家指正
    我的 云寻觅 搜索引擎 开始公测,前天开始开发,昨天买域名,今天发布在本机,请各路专家指正! 顺便开源!
    20071225是个值得纪念的日子,我用纯C语言开发的空间首次上线测试!
    给C# .NET 的兄弟们做点小贡献 NoSql LevelDB .net 移植版 普通PC 100万条数据插入不超过4秒
    开源:给每个文档计算一个指纹,然后用指纹进行相似度的计算 含源码和可执行程序
    国内首款完全由国人自主研发的开源云平台 BDC3.0 详解
  • 原文地址:https://www.cnblogs.com/chilumanxi/p/5136092.html
Copyright © 2011-2022 走看看