zoukankan      html  css  js  c++  java
  • FZU2150(KB1-I)

    Fire Game

    Accept: 1955    Submit: 6880
    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 //2017-02-28
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <queue>
     6 
     7 using namespace std;
     8 
     9 char grid[15][15], tmp[15][15];
    10 int dx[4] = {0, 0, 1, -1};
    11 int dy[4] = {1, -1, 0, 0};
    12 int book[15][15], answer = 0x3f3f3f3f;
    13 struct node
    14 {
    15     int x, y, step;
    16 };
    17 
    18 void bfs(int n, int m, int x1, int y1, int x2, int y2)
    19 {
    20     for(int i = 0; i < n; i++)
    21       for(int j = 0; j < m; j++)
    22         tmp[i][j] = grid[i][j];
    23     int x, y, step, ans = 0;
    24     tmp[x1][y1] = '*';
    25     tmp[x2][y2] = '*';
    26     queue<node> q;
    27     node tmpnode;
    28     tmpnode.x = x1;
    29     tmpnode.y = y1;
    30     tmpnode.step = 0;
    31     q.push(tmpnode);
    32     tmpnode.x = x2;
    33     tmpnode.y = y2;
    34     q.push(tmpnode);
    35     while(!q.empty())
    36     {
    37         x = q.front().x;
    38         y = q.front().y;
    39         step = q.front().step;
    40         for(int i = 0; i < 4; i++)
    41         {
    42             int nx = x + dx[i];
    43             int ny = y + dy[i];
    44             if(nx>=0&&nx<n&&ny>=0&&ny<m&&tmp[nx][ny]=='#')
    45             {
    46                 tmpnode.x = nx;
    47                 tmpnode.y = ny;
    48                 tmpnode.step = step+1;
    49                 ans = step+1;
    50                 tmp[nx][ny] = '*';
    51                 q.push(tmpnode);
    52             }
    53         }
    54         q.pop();
    55     }
    56     for(int i = 0; i < n; i++)
    57       for(int j = 0; j < m; j++)
    58         if(tmp[i][j] == '#'){
    59             return;
    60         }
    61     if(ans < answer)answer = ans;
    62 }
    63 
    64 int main()
    65 {
    66     int T, n, m, cnt;
    67     cin>>T;
    68     for(int kase = 1; kase <= T; kase++)
    69     {
    70         cin>>n>>m;
    71         cnt = 0;
    72         for(int i = 0; i < n; i++)
    73           for(int j = 0; j < m; j++)
    74           {
    75               cin>>grid[i][j];
    76               if(grid[i][j] == '#')cnt++;
    77           }
    78         answer = 0x3f3f3f3f;
    79         for(int i = 0; i < n; i++)
    80               for(int j = 0; j < m; j++)
    81             {
    82                 if(grid[i][j] == '#'){
    83                     for(int i2 = i; i2 < n; i2++)
    84                       for(int j2 = 0; j2 < m; j2++){
    85                           if(i==i2 && j2 <= j)continue;
    86                           if(grid[i2][j2] == '#')bfs(n, m, i, j, i2, j2);
    87                       }
    88                 }
    89             }
    90         if(answer == 0x3f3f3f3f)answer = -1;
    91         if(cnt==1)answer=0;
    92         cout<<"Case "<<kase<<": "<<answer<<endl;
    93     }
    94     return 0;
    95 }
  • 相关阅读:
    NOIp 图论算法专题总结 (3):网络流 & 二分图 简明讲义
    zkw 线段树
    NOIp 图论算法专题总结 (2)
    NOIp 数据结构专题总结 (2):分块、树状数组、线段树
    单调栈、单调队列 思路及例题
    java自动装箱和拆箱
    HashMap和HashTable的异同点
    HttpServletRequest. getParameter获取的参数格式
    关于交换函数(1)
    std::vector::iterator重载了下面哪些运算符
  • 原文地址:https://www.cnblogs.com/Penn000/p/6479474.html
Copyright © 2011-2022 走看看