Problem 2150 fire gameAccept: 392 submit: 1502
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 wowould never burn out and the empty grid wowould 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 input43 3. #. ###. #. 3. #. #. #. #. 3 3... #. #... 3 3 ###.. ##. # sample outputcase 1: 1 case 2:-1 case 3: 0 case 4: 2 // BFS of two vertices, because N and m are relatively small, therefore, you can obtain the minimum value by enumerating any two points. Use D [N] [m] to represent the minimum number of steps at this point. First, the initial value is infinity, and then the time duration is updated with a value smaller than him. This is the key to the array. A value is returned for each traversal. If it is INF, it means it is not convenient.
# Include <cstdio> # include <queue> # include <cstring> # include <algorithm> using namespace STD; # define INF 10000 struct point {int A, B ;}; int n, m, d [11] [11], dir [4] [2] = {-, 0,-1 }; char map [11] [11]; int BFS (point X, point y) {queue <point> q; point T, TT; q. push (x); q. push (y); memset (D, INF, sizeof (d); D [X. a] [x. b] = 0; d [Y. a] [Y. b] = 0; while (! Q. empty () {T = Q. front (); q. pop (); For (INT I = 0; I <4; I ++) {TT. A = T. A + dir [I] [0]; TT. B = T. B + dir [I] [1]; If (TT. a> = 0 & TT. A <n & TT. b> = 0 & TT. B <M & map [TT. a] [TT. b] = '#' & D [TT. a] [TT. b]> d [T. a] [T. b] + 1) {d [TT. a] [TT. b] = d [T. a] [T. b] + 1; q. push (TT) ;}}int res = 0; // enumerate two vertices at a time. If the traversal is incomplete, then res must be equal to INF for (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) {// printf ("% d", d [I] [J]); If (Map [I] [J] = '#') RES = max (Res, d [I] [J]);} // printf ("\ n");} return res;} int main () {// freopen ("a.txt ", "r", stdin); int T, CAS = 1; point S1, S2; scanf ("% d", & T); While (t --) {int COUNT = 0; scanf ("% d", & N, & M); getchar (); For (INT I = 0; I <N; I ++) {scanf ("% s", map [I]); // printf ("% s \ n", map [I]); for (Int J = 0; j <m; j ++) if (Map [I] [J] = '#') Count ++ ;} printf ("case % d:", CAS ++); If (count <= 2) {printf ("0 \ n"); continue;} int CNT = inf; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) {If (Map [I] [J] = '#') {for (INT L = 0; L <n; l ++) {for (int K = 0; k <m; k ++) {If (L <I & K <= J) continue; If (Map [l] [k] = '#') {s1.a = I; s1. B = J; s2.a = L; s2. B = K; int ans = BFS (S1, S2); CNT = min (ANS, CNT) ;}}}}}if (CNT = inf) printf ("-1 \ n "); // If the enumeration of any two points CNT is INF input-1 else printf ("% d \ n", CNT);} return 0 ;}