Fzu 2150 Fire Game

Source: Internet
Author: User

Fire GameTime limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticeFzu 2150

Description

Fat brother and Maze is playing a kind of special (hentai) game on an n*m board (N rows, M columns). At the beginning, each of the grids of this board are consisting of grass or just empty and then they start-to-fire all the grass. Firstly they choose, grids which is 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 are adjacent to this grid would 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 the no new grid get fire. If then all the grid which be consisting of grass is get fired, Fat brother and Maze would 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 this grass in the board would never burn out and the empty grid would never get fire.

Note that the both grids they choose can be the the same.

Input

The first line of the date is a integer T, which is the number of the text cases.

Then T cases follow, each case contains the 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 this there is at least one grid which are consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

Output

For each case, output of 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 test instructions: Give a m*n figure, ' # ' means lawn, '. ' Indicates the open space, then can choose to fire in any of the two lawn lattice, fire every 1 s will spread to the surrounding four lattice, ask to choose those two points make burning all the lawn spend the least time
#include <iostream>#include<cstdio>#include<cstring>#include<queue>#include<cstdlib>#include<cmath>#include<cctype>#defineN 210#defineMAXN 0XFFFFFFFusing namespacestd;intm, N, T;CharMaps[n][n];BOOLF[n][n];intdir[4][2] = {0,1,1,0,0,-1, -1,0};structnode{intx, y; intStep;} a[ the];BOOLFire () { for(inti =0; I < m; i++)     for(intj =0; J < N; J + +) {        if(Maps[i][j] = ='#'&&!F[i][j]) {            return false; }    }    return true;}intBFS (node s, node e) {inti; Queue<node>Q;    Q.push (s);    Q.push (e); F[S.X][S.Y]= F[e.x][e.y] =true;  while(Q.size ()) {s=Q.front ();        Q.pop (); E=s;  for(i =0; I <4; i++) {e.x= S.x + dir[i][0]; E.y= S.y + dir[i][1]; E.step= S.step +1; if(E.x >=0&& e.x < m && e.y >=0&& e.y < n && maps[e.x][e.y] = ='#'&&!F[e.x][e.y]) {F[E.X][E.Y]=true;            Q.push (e); }        }    }    returnS.step;}intMain () {scanf ("%d", &T);  for(intK =1; K <= T; k++) {scanf ("%d%d", &m, &N); intb =0, ans =MAXN;  for(inti =0; I < m; i++) {//process image, maps value is true for lawnscanf" ");  for(intj =0; J < N; J + +) {scanf ("%c", &Maps[i][j]); if(Maps[i][j] = ='#') {a[b].x=i; A[b].y=J; A[b++].step =0; }            }        }         for(inti =0; I < b; i++) {             for(intj = i; J < b; J + +) {memset (F,0,sizeof(f)); intTemp_ans =BFS (A[i], a[j]); if(Ans > Temp_ans &&Fire ()) {ans=Temp_ans; }            }        }        if(ans = =maxn) printf ("Case %d: -1\n", K); Elseprintf ("Case %d:%d\n", k, ans); }    return 0;}

Fzu 2150 Fire Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.