• (广搜)Fire Game -- FZU -- 2150


    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I

    Fire Game
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     
    就一个简单的广搜题, 害的我A了2两天, 后来才发现是a[]数组开小了, -_- (不开心)
     
     
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<queue>
    using namespace std;
    
    #define INF 0x3f3f3f3f
    #define N 20
    
    struct node
    {
        int x, y, step;
    }a[N*N];
    
    int n, m, t;
    char G[N][N];
    int f[N][N];
    int dir[4][2] = {{-1,0},{0,-1},{1,0},{0,1}};
    
    int BFS(node s1, node s2)
    {
        node p, q;
    
        queue<node>Q;
        Q.push(s1);
        Q.push(s2);
    
        t = 0;
        f[s1.x][s1.y] = 1;
        f[s2.x][s2.y] = 1;
    
        while(Q.size())
        {
            p = Q.front(), Q.pop();
    
            for(int i=0; i<4; i++)
            {
                q.x = p.x + dir[i][0];
                q.y = p.y + dir[i][1];
    
                if(q.x>=0 && q.x<n && q.y>=0 && q.y<m && G[q.x][q.y]=='#' && !f[q.x][q.y])
                {
                    f[q.x][q.y] = 1;
                    q.step = p.step + 1;
                    t = max(t, q.step);
                    Q.push(q);
                }
            }
        }
    
        return t;
    }
    
    int Judge()
    {
        int i, j;
    
        for(i=0; i<n; i++)
        for(j=0; j<m; j++)
        {
            if(G[i][j]=='#' && !f[i][j])
                return 0;
        }
        return 1;
    }
    
    int main()
    {
        int T, iCase=1;
        scanf("%d", &T);
        while(T--)
        {
            int i, j, k=0;
            scanf("%d%d", &n, &m);
    
            memset(G, 0, sizeof(G));
            for(i=0; i<n; i++)
            {
                scanf("%s", G[i]);
                for(j=0; j<m; j++)
                {
                   if(G[i][j]=='#')
                   {
                       a[k].x=i, a[k].y=j, a[k].step=0;
                       k++;
                   }
                }
            }
    
            int ans=INF, Step;
            for(i=0; i<k; i++)
            for(j=i; j<k; j++)
            {
                memset(f, 0, sizeof(f));
                Step = BFS(a[i], a[j]);
                if(Step<ans && Judge())
                    ans = Step;
            }
    
            printf("Case %d: ", iCase++);
            if(ans==INF)
                printf("-1
    ");
            else
                printf("%d
    ", ans);
        }
        return 0;
    }
    View Code
     
     
    勿忘初心
  • 相关阅读:
    java:LeakFilling (Linux)
    java:redis(redis安装配置,redis的伪集群配置)
    java:Linux(简单命令,远程ssh使用hostname访问,.免密钥登录配置)
    java:easyui(重点示例)
    任意文件下载漏洞学习
    Centos7 nginx报错403 forbidden
    Centos7 nginx提示错误 Access denied.
    Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment
    PHP载入GIF图像造成服务器宕机(CVE-2018-5711)的漏洞复现
    Python问题:'Nonetype' object is not iterable
  • 原文地址:https://www.cnblogs.com/YY56/p/4927354.html
Copyright © 2020-2023  润新知