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
  • 相关阅读:
    第15.9节 PyQt学习入门:使用Qt Designer进行GUI设计的步骤
    PyQt学习随笔:Model/View开发时在view数据项中设置不同角色数据的方法
    PyQt学习随笔:Model/View开发时从Model相关类派生自定义类需要注意的问题
    PyQt学习随笔:重写setData方法截获Model/View中视图数据项编辑的注意事项
    PyQt学习随笔:Model/View中视图数据项编辑变动实时获取变动数据的方法
    Python中高级知识(非专题部分)学习随笔
    clistctrl 虚拟列表
    数字图象处理图片库
    MFC中char*,string和CString之间的转换
    图像分割之(四)OpenCV的GrabCut函数使用和源码解读
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5001900.html
Copyright © 2011-2022 走看看