zoukankan      html  css  js  c++  java
  • (广搜)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
     
     
    勿忘初心
  • 相关阅读:
    ASP.NET AJAX入门系列(1):概述
    ASP.NET中常用的文件上传下载方法
    Asp.net中DataBinder.Eval用法的总结
    ASP.NET AJAX入门系列(6):UpdateProgress控件简单介绍
    ASP.NET AJAX入门系列(8):自定义异常处理
    Javascrip常用语句
    26个常用的方法优化 ASP.NET 的性能
    JavaScript倒计时组件
    jQuery.buildFragment源码分析
    jQuery.Callbacks源码解读
  • 原文地址:https://www.cnblogs.com/YY56/p/4927354.html
Copyright © 2011-2022 走看看