FZU 2150 Fire Game

Fire Game
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 #include <stdio.h>
  2 #include <string.h>
  3 #include <queue>
  4 #include <algorithm>
  5 using namespace std;
  6 
  7 const int inf=0x3f3f3f3f;
  8 
  9 int T,ca=1;
 10 int n,m;
 11 char mapp[12][12];
 12 int num[12][12],s1,s2,s,d[12][12],sum;
 13 int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
 14 
 15 struct Node
 16 {
 17     int r;
 18     int c;
 19 };
 20 
 21 int dfs(int x,int y)
 22 {
 23     int i,j;
 24     num[x][y]=s;
 25     for(i=0;i<4;i++)
 26     {
 27         int nx=x+dx[i],ny=y+dy[i];
 28         if(1<=nx && nx<=n && 1<=ny && ny<=m && mapp[nx][ny]=='#' && num[nx][ny]==0)
 29         {
 30             dfs(nx,ny);
 31         }
 32     }
 33 }
 34 
 35 queue <Node> que;
 36 int bfs()
 37 {
 38     int i,j,k;
 39     while(que.size())
 40     {
 41         Node now=que.front();que.pop();
 42 
 43         for(i=0;i<4;i++)
 44         {
 45             int nx=now.r+dx[i],ny=now.c+dy[i];
 46             if(1<=nx && nx<=n && 1<=ny && ny<=m && mapp[nx][ny]=='#' && d[nx][ny]==inf)
 47             {
 48                 Node nex;
 49                 nex.r=nx,nex.c=ny;
 50                 que.push(nex);
 51                 d[nx][ny]=d[now.r][now.c]+1;
 52                 if(d[nx][ny]>sum)
 53                     sum=d[nx][ny];
 54             }
 55         }
 56     }
 57 }
 58 
 59 int main()
 60 {
 61     int i,j,k,ii,jj;
 62     scanf("%d",&T);
 63     while(T--)
 64     {
 65         s=0;
 66         memset(num,0,sizeof(num));
 67         scanf("%d %d",&n,&m);
 68         getchar();
 69         for(i=1;i<=n;i++)
 70         {
 71             for(j=1;j<=m;j++)
 72             {
 73                 scanf("%c",&mapp[i][j]);
 74             }
 75             getchar();
 76         }
 77         for(i=1;i<=n;i++)
 78         {
 79             for(j=1;j<=m;j++)
 80             {
 81                 if(mapp[i][j]=='#' && num[i][j]==0)
 82                 {
 83                     s++;
 84                     dfs(i,j);
 85                 }
 86             }
 87         }
 88 
 89         if(s>2)
 90         {
 91             printf("Case %d: -1
",ca++);
 92         }
 93         else if(s==2)
 94         {
 95             s1=inf,s2=inf;
 96             for(i=1;i<=n;i++)
 97             {
 98                 for(j=1;j<=m;j++)
 99                 {
100                     if(mapp[i][j]=='#')
101                     {
102                         memset(d,inf,sizeof(d));
103                         while(que.size())
104                             que.pop();
105                         Node begin;
106                         begin.r=i,begin.c=j;
107                         que.push(begin);
108                         d[i][j]=0;
109                         sum=0;
110                         bfs();
111     
112                         if(num[i][j]==1 && sum<s1)
113                         {
114                             s1=sum;
115                         }
116                         if(num[i][j]==2 && sum<s2)
117                         {
118                             s2=sum;
119                         }
120                     }
121                 }
122             }
123             printf("Case %d: %d
",ca++,max(s1,s2));
124         }
125         else if(s==1)
126         {
127             int ss=inf;
128             for(i=1;i<=n;i++)
129                 for(j=1;j<=m;j++)
130                 {
131                     if(mapp[i][j]=='#')
132                     {
133                         for(ii=1;ii<=n;ii++)
134                             for(jj=1;jj<=m;jj++)
135                             {
136                                     if(mapp[ii][jj]=='#')
137                                     {
138                                         memset(d,inf,sizeof(d));
139                                         while(que.size())
140                                             que.pop();
141                                         Node begin;
142                                         begin.r=i,begin.c=j;
143                                         que.push(begin);
144                                         d[i][j]=0;
145                                         begin.r=ii,begin.c=jj;
146                                         que.push(begin);
147                                         d[ii][jj]=0;
148                                         sum=0;
149                                         bfs();
150                                         if(sum<ss)
151                                             ss=sum;
152                                     }
153                             }
154                     }
155                 }
156             printf("Case %d: %d
",ca++,ss);
157         }
158         else if(s==0)
159             printf("Case %d: 0
",ca++);
160     }
161     return 0;
162 }
View Code
原文地址:https://www.cnblogs.com/cyd308/p/4771420.html