zoukankan      html  css  js  c++  java
  • foj 2150 Fire Game(bfs暴力)

     
     

     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

    暴力枚举两点,bfs统计,最后求最小值。

      1 #pragma comment(linker, "/STACK:1024000000,1024000000")
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<math.h>
      7 #include<algorithm>
      8 #include<queue>
      9 #include<set>
     10 #include<bitset>
     11 #include<map>
     12 #include<vector>
     13 #include<stdlib.h>
     14 #include <stack>
     15 using namespace std;
     16 #define PI acos(-1.0)
     17 #define max(a,b) (a) > (b) ? (a) : (b)
     18 #define min(a,b) (a) < (b) ? (a) : (b)
     19 #define ll long long
     20 #define eps 1e-10
     21 #define MOD 1000000007
     22 #define N 16
     23 #define inf 1<<26
     24 int n,m;
     25 char mp[N][N];
     26 int vis[N][N];
     27 int dirx[]={0,0,-1,1};
     28 int diry[]={-1,1,0,0};
     29 struct Node{
     30    int x,y;
     31    int t;
     32 }node[N*N];
     33 
     34 int bfs(Node a,Node b){
     35    queue<Node>q;
     36    q.push(a);
     37    q.push(b);
     38    vis[a.x][a.y]=1;
     39    vis[b.x][b.y]=1;
     40    int T=0;
     41    while(!q.empty()){
     42       Node tmp=q.front();
     43       q.pop();
     44       Node cnt;
     45       T=max(T,tmp.t);
     46       for(int i=0;i<4;i++){
     47          cnt.x=tmp.x+dirx[i];
     48          cnt.y=tmp.y+diry[i];
     49          if(cnt.x<0 || cnt.x>=n || cnt.y<0 || cnt.y>=m) continue;
     50          if(vis[cnt.x][cnt.y]) continue;
     51          if(mp[cnt.x][cnt.y]=='.') continue;
     52          vis[cnt.x][cnt.y]=1;
     53          cnt.t=tmp.t+1;
     54          q.push(cnt);
     55       }
     56    }
     57    return T;
     58 }
     59 
     60 int main()
     61 {
     62    int t,ac=0;
     63    scanf("%d",&t);
     64    while(t--){
     65       int num=0;
     66       scanf("%d%d",&n,&m);
     67       for(int i=0;i<n;i++){
     68          scanf("%s",mp[i]);
     69          for(int j=0;j<m;j++){
     70             if(mp[i][j]=='#'){
     71                node[num].x=i;
     72                node[num].y=j;
     73                node[num++].t=0;
     74             }
     75          }
     76       }
     77       
     78       printf("Case %d: ",++ac);
     79       if(num<=2){
     80          printf("0
    ");
     81          continue;
     82       }
     83       
     84       int ans=inf;
     85       for(int i=0;i<num;i++){
     86          for(int j=i;j<num;j++){
     87             int flag=1;
     88             memset(vis,0,sizeof(vis));
     89             int w=bfs(node[i],node[j]);
     90             //printf("===%d
    ",w);
     91             for(int k=0;k<n;k++){
     92                for(int l=0;l<m;l++){
     93                    if(mp[k][l]=='#' && vis[k][l]==0){
     94                       flag=0;
     95                       break;
     96                    }
     97                }
     98                if(flag==0){
     99                   break;
    100                }
    101             }
    102             if(flag){
    103                ans=min(ans,w);
    104             }
    105          }
    106       }
    107       
    108      if(ans!=inf){
    109         printf("%d
    ",ans);
    110      }
    111      else{
    112         printf("-1
    ");
    113      }
    114    }
    115     return 0;
    116 }
    View Code
  • 相关阅读:
    Web Api系列教程第2季(OData篇)(一)——OData简介和一个小应用
    漫步ASP.NET MVC的处理管线
    使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【十】——使用CacheCow和ETag缓存资源
    工具分享——将C#文档注释生成.chm帮助文档
    使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【外传】——Attribute Routing
    使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【九】——API变了,客户端怎么办?
    使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【八】——Web Api的安全性
    C#基础——谈谈.NET异步编程的演变史
    使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【七】——实现资源的分页
    使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【六】——实现资源间的关联
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5001900.html
Copyright © 2011-2022 走看看