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\n");
 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\n",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\n",ans);
110      }
111      else{
112         printf("-1\n");
113      }
114    }
115     return 0;
116 }
View Code

 

 

 

 

 

转载于:https://www.cnblogs.com/UniqueColor/p/5001900.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值