Fzu 2150 Fire Game

Source: Internet
Author: User

problem 2150 Fire Gameaccept:864 submit:3207
Time limit:1000 mSec Memory limit:32768 KB problem 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 Input43 3.#.###.#.3 3.#.#.#.#.3 3...#.# ... 3 3###. ##.# Sample outputcase 1:1case 2: -1case 3:0case 4:2 dfs+bfs do. The code is a bit lame. Idea: First DFS chunked, then divided into 2 cases to discuss the number of blocks for the 1 o'clock enumeration of two fire points for the minimum time, the number of blocks is 2 o'clock each block of the minimum time, in these two minimum time to take the largest.
/************************************************author:p k29created time:2015/8/18 16:50:09File name:4.c pp*************************************************/#include<iostream>#include<cstring>#include<cstdlib>#include<stdio.h>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<iomanip>#include<list>#include<deque>#include<stack>#defineull unsigned long Long#definell Long Long#defineMoD 90001#defineINF 0x3f3f3f3f#defineMAXN 10000+10#defineCLE (a) memset (A,0,sizeof (a))Constull inf = 1LL << A;Const Doubleeps=1e-5;using namespacestd;structnode{intx,y,dist;};BOOLcmpintAintb) {    returnA>b;}Charmp[ -][ -];intn,m,vis[ -][ -];intdir[4][2]={1,0,0,1,0,-1,-1,0};intCNT;voidDfsintXinty) {    if(cnt>2)return ; Vis[x][y]=1; Mp[x][y]=Char(cnt+'0');  for(intI=0;i<4; i++){        intnx=x+dir[i][0]; intny=y+dir[i][1]; if(!vis[nx][ny]&&nx<n&&ny<m&&nx>=0&&ny>=0&&mp[nx][ny]=='#') {Vis[nx][ny]=1;        DFS (NX,NY); }    }}intBFS (node S,Charc) {    intvist[ the][ the]={0}; VIST[S.X][S.Y]=1; Queue<node>Q;    Q.push (s);    Node u,v;  while(!Q.empty ()) {u=Q.front (), Q.pop ();  for(intI=0;i<4; i++) {v.x=u.x+dir[i][0]; V.y=u.y+dir[i][1]; if(v.x<n&&v.x>=0&&v.y<m&&v.y>=0&&mp[v.x][v.y]==c&&!Vist[v.x][v.y]) {V.dist=u.dist+1;                Q.push (v); VIST[V.X][V.Y]=1; }        }        if(Q.empty ())returnu.dist; }}intBFS (node S1,node s2,Charc) {    intvist[ the][ the]={0}; VIST[S1.X][S1.Y]=1; VIST[S2.X][S2.Y]=1; Queue<node>Q;    Q.push (S1);    Q.push (S2);    Node u,v;  while(!Q.empty ()) {u=Q.front (), Q.pop ();  for(intI=0;i<4; i++) {v.x=u.x+dir[i][0]; V.y=u.y+dir[i][1]; if(v.x<n&&v.x>=0&&v.y<m&&v.y>=0&&mp[v.x][v.y]==c&&!Vist[v.x][v.y]) {V.dist=u.dist+1;                Q.push (v); VIST[V.X][V.Y]=1; }        }        if(Q.empty ())returnu.dist; }}intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin); #endif    //freopen ("OUT.txt", "w", stdout);    intT; CIN>>T; intcas=0;  while(t--) {scanf ("%d%d",&n,&m);  for(intI=0; i<n;i++) scanf ("%s", Mp[i]); CNT=0;        CLE (VIS);  for(intI=0; i<n;i++)             for(intj=0; j<m;j++){                if(!vis[i][j]&&mp[i][j]=='#') {CNT++;                DFS (I,J); }            }        if(cnt>2) {printf ("Case %d:%d\n", ++cas,-1);Continue; }        Else if(cnt==0) {printf ("Case %d:%d\n", ++cas,0);Continue; }        Else if(cnt==1) {cle (VIS);intmin=INF;  for(intI=0; i<n;i++)                 for(intj=0; j<m;j++)                     for(intk=0; k<n;k++)                         for(intp=0;p <m;p++){                            if(mp[i][j]=='1'&&mp[k][p]=='1') {node s1,s2; s1.x=i,s1.y=j,s1.dist=0; s2.x=k,s2.y=p,s2.dist=0; Min=min (Bfs (S1,S2,'1'), Min); }} printf ("Case %d:%d\n", ++cas,min);Continue; }        Else{cle (VIS); intMin1=inf,min2=INF;  for(intI=0; i<n;i++)                 for(intj=0; j<m;j++){                    if(mp[i][j]=='1'&&!Vis[i][j]) {Vis[i][j]=1; Node S;s.x=i,s.y=j,s.dist=0; Min1=min (BFS (s),'1'), Min1); }                    Else if(mp[i][j]=='2'&&!Vis[i][j]) {Vis[i][j]=1; Node S;s.x=i,s.y=j,s.dist=0; Min2=min (BFS (s),'2'), Min2); }} printf ("Case %d:%d\n", ++cas,max (min1,min2));Continue; }    }    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.