FZU 2150 fire game (bfs)

Problem 2150 Fire Game

Accept: 2133    Submit: 7494
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
 
感觉bfs很容易错在细节上,一定要注意细节!
刚开始写了一个队列的,后来发现需要两个队列。。。结果又发现一个队列就够了!
另外要注意特判grass数量<=2的情况;
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #define INF 0x3f3f3f
  6 
  7 using namespace std;
  8 
  9 struct node{
 10     int a,b;
 11     int step;
 12 };
 13 node que1[1000];
 14 int start1=0,endd1=0;
 15 node grass[101];
 16 int couGrass=0;
 17 char ch[15][15];
 18 int directX[4]={-1,1,0,0};
 19 int directY[4]={0,0,-1,1};
 20 int t,n,m;
 21 
 22 int bfs(int first,int second,int cougra){
 23     int nowCouGra=0;
 24     int nowstep=0;
 25     start1=0,endd1=0;
 26     char vis[15][15];
 27     for(int i=0;i<n;i++){
 28         for(int j=0;j<m;j++){
 29             vis[i][j]=ch[i][j];
 30         }
 31     }
 32     node t1,t2;
 33     t1.a=grass[first].a;
 34     t1.b=grass[first].b;
 35     t1.step=0;
 36     t2.a=grass[second].a;
 37     t2.b=grass[second].b;
 38     t2.step=0;
 39     que1[endd1++]=t1;
 40     que1[endd1++]=t2;
 41     vis[t1.a][t1.b]='.';
 42     vis[t2.a][t2.b]='.';
 43     nowCouGra++,nowCouGra++;
 44     while(start1<endd1){
 45             node now=que1[start1++];
 46             for(int i=0;i<4;i++){
 47                 node next;
 48                 next.a=now.a+directX[i];
 49                 next.b=now.b+directY[i];
 50                 next.step=now.step+1;
 51                 if(next.a>=0&&next.a<n&&next.b>=0&&next.b<m){
 52                     if(vis[next.a][next.b]=='#'){
 53                         vis[next.a][next.b]='.';
 54                         que1[endd1++]=next;
 55                         nowstep=max(nowstep,next.step);
 56                         nowCouGra++;
 57                     }
 58                 }
 59             }
 60         if(nowCouGra==cougra){
 61             return nowstep;
 62         }
 63     }
 64     return INF;
 65 }
 66 
 67 int main()
 68 {
 69     scanf("%d",&t);
 70     for(int ii=0;ii<t;ii++){
 71         couGrass=0;
 72         scanf("%d %d",&n,&m);
 73         getchar();
 74         for(int j=0;j<n;j++){
 75             for(int k=0;k<m;k++){
 76                 scanf("%c",&ch[j][k]);
 77                 if(ch[j][k]=='#'){
 78                     grass[couGrass].a=j;
 79                     grass[couGrass++].b=k;
 80                 }
 81             }
 82             getchar();
 83         }
 84         if(couGrass<=2){
 85             printf("Case %d: 0\n",ii+1);
 86             continue;
 87         }
 88         int ans=INF;
 89         for(int i=0;i<couGrass;i++){
 90             for(int j=i+1;j<couGrass;j++){
 91                 ans=min(ans,bfs(i,j,couGrass));
 92             }
 93         }
 94         if(ans==INF){
 95             printf("Case %d: -1\n",ii+1);
 96         }else{
 97             printf("Case %d: %d\n",ii+1,ans);
 98         }
 99 
100     }
101     return 0;
102 }
View Code

 

posted @ 2017-04-23 18:06  多一份不为什么的坚持  阅读(191)  评论(0编辑  收藏  举报