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
Sample Output
1 /************************************************************************* 2 > File Name: code/2015summer/searching/I.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年07月27日 星期一 18时25分27秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #define y0 abc111qqz 21 #define y1 hust111qqz 22 #define yn hez111qqz 23 #define j1 cute111qqz 24 #define tm crazy111qqz 25 #define lr dying111qqz 26 using namespace std; 27 #define REP(i, n) for (int i=0;i<int(n);++i) 28 typedef long long LL; 29 typedef unsigned long long ULL; 30 const int N=1E2+5; 31 const int inf = 0x7fffffff; 32 int x[N],y[N]; 33 int cnt; 34 int m,n; 35 char maze[11][11]; 36 int d[11][11]; 37 int dx[4]={0,0,-1,1}; 38 int dy[4]={1,-1,0,0}; 39 int k; 40 41 bool ok (int x,int y) 42 { 43 if (x>=0&&x<n&&y>=0&&y<m&&maze[x][y]=='#'&&d[x][y]==-1) 44 return true; 45 return false; 46 } 47 int bfs(int x1,int y1,int x2,int y2) 48 { 49 memset(d,-1,sizeof(d)); 50 queue<int>x; 51 queue<int>y; 52 x.push(x1); 53 x.push(x2); 54 y.push(y1); 55 y.push(y2); 56 d[x1][y1]=0; 57 d[x2][y2]=0; 58 cnt = 2; 59 while (!x.empty()&&!y.empty()) 60 { 61 int px = x.front();x.pop(); 62 int py = y.front();y.pop(); 63 // cout<<"px:"<<px<<" py:"<<py<<endl; 64 int tx,ty; 65 for ( int i = 0 ; i < 4 ; i++ ) 66 { 67 int nx = px + dx[i]; 68 int ny = py + dy[i]; 69 if (ok(nx,ny)) 70 { 71 d[nx][ny]=d[px][py]+1; 72 x.push(nx); 73 y.push(ny); 74 cnt++; 75 tx = nx; 76 ty = ny; 77 } 78 } 79 if (cnt>=k) 80 { 81 return d[tx][ty]; //最后一次烧到的点一定是最远的点 82 } 83 84 } 85 return inf; 86 } 87 int main() 88 { 89 int T; 90 cin>>T; 91 int cas = 0; 92 while (T--) 93 { 94 cas++; 95 scanf("%d %d",&n,&m); 96 for ( int i = 0 ; i < n ; i++ ) 97 { 98 scanf("%s",maze[i]); 99 } 100 k = 0 ; 101 for ( int i = 0 ; i < n ; i++ ) 102 { 103 for ( int j = 0 ; j < m ; j++ ) 104 { 105 if (maze[i][j]=='#') 106 { 107 k++; 108 x[k]=i; 109 y[k]=j; 110 } 111 } 112 } 113 if (k<=2) 114 { 115 116 printf("Case %d: %d ",cas,0); 117 continue; 118 } 119 int ans = inf; 120 for ( int i = 1 ; i <= k ; i++ ) 121 { 122 for ( int j = i ; j <= k ; j++ ) 123 { 124 cnt = 0 ; 125 ans = min(ans,bfs(x[i],y[i],x[j],y[j])); 126 } 127 } 128 if (ans!=inf) 129 printf("Case %d: %d ",cas,ans); 130 else printf("Case %d: %d ",cas,-1); 131 132 } 133 134 return 0; 135 }