Fzu 2150 Fire Game (high posture bfs--two starting points)

Source: Internet
Author: User

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

43 3.#.###.#.3 3.#.#.#.#.3 3...#.# ... 3 3###. ##.#

Sample Output

Case 1:1case 2: -1case 3:0case 4:2

Give you a piece of land, there is open space, there are haystack, let you choose two haystack for ignition, burning the haystack will ignite up and down around the adjacent haystack, each time the ignition cost 1s time, ask you to spend at least how long the haystack are lit, if do not output-1.
The problem at the beginning of the wrong posture, think wrong, first BFS to find connected blocks, if the number of connected blocks is more than 3 direct GG, otherwise in the known connected block to find a depth ... 283 lines of code have been directly hung out.
However, the correct idea is Jiangzi: In time there is only one connected block, we can also choose two ignition points to reduce the time.
So direct violence enumeration every two haystack, the two points into the BFS queue, two start BFS, to see how many points at this point how much of the BFS time (that is, Q in the last pop out of the elements of the depth), and then determine whether the choice of the two points can be the whole point of the haystack.
PS: The last half hour of the problem is simply because there is no initialization, and the habit of developing good habits every time before each test run a line init () ...
The code is as follows:
1#include <iostream>2#include <algorithm>3#include <string>4#include <cstring>5#include <cmath>6#include <cstdio>7#include <queue>8#include <vector>9 using namespacestd;Ten #defineINF 0x3f3f3f3f One intn,m; A BOOLvis[ the][ the]; - Chargrid[ the][ the]; - intCasee=0; the intans=inf; - structnode - { -     intx,y,depth; + }; -Vector <node>Grass; + BOOLCheck (intXinty) A { at     if(!vis[x][y]&&grid[x][y]=='#'&&x>=0&&x<n&&y>=0&&y<m) -     return true; -     Else -     return false; - } - BOOLjudge () in { -      for(intI=0; i<n;++i) { to          for(intj=0; j<m;++j) { +             if(grid[i][j]=='#'&&!Vis[i][j]) -             return false; the         } *     } $     return true;Panax Notoginseng } - voidInit () the { + grass.clear (); Amemset (Vis,false,sizeofvis); the } + intBFS (Node n1,node n2) - { $Queue <node>Q; $memset (Vis,false,sizeofvis); -      while(!q.empty ()) Q.pop (); - Q.push (N1); the Q.push (n2); -     intdepthest=0;Wuyi      while(!q.empty ()) the     { -Node now=Q.front (); Wu Q.pop (); -         if(Vis[now.x][now.y]) About         Continue; $vis[now.x][now.y]=true; -depthest=now.depth; -         if(Check (now.x-1, now.y)) -         { ANode nxt=Now ; +nxt.x--; thenxt.depth++; - Q.push (NXT); $         } the         if(Check (now.x+1, now.y)) the         { theNode nxt=Now ; thenxt.x++; -nxt.depth++; in Q.push (NXT); the         } the         if(Check (now.x,now.y-1)) About         { theNode nxt=Now ; thenxt.y--; thenxt.depth++; + Q.push (NXT); -         } the         if(Check (now.x,now.y+1))Bayi         { theNode nxt=Now ; thenxt.y++; -nxt.depth++; - Q.push (NXT); the         } the     } the     returndepthest; the } - intMain () the { the     //freopen ("De.txt", "R", stdin); the     intT;94scanf"%d",&t); the      while(t--) the     { the init ();98ans=inf; Aboutscanf"%d%d",&n,&m); -          for(intI=0; i<n;++i)101scanf"%s", Grid[i]);102          for(intI=0; i<n;++i) {103              for(intj=0; j<m;++j) {104                 if(grid[i][j]=='#'){ the node G;106g.x=i;107g.y=J;108G.depth=0;109 Grass.push_back (g); the                 }111             } the         }113          for(intI=0; I<grass.size (); + +i) the         { the              for(intJ=i;j<grass.size (); + +j) the             {117Grass[i].depth=0;118Grass[j].depth=0;119                 inttemp=min (BFS (grass[i],grass[j]), ans); -                 if(judge ())121ans=min (ans,temp);122             }123         }124printf"Case %d:",++casee); the         if(ans==inf)126printf"-1\n");127         Else -printf"%d\n", ans);129     } the     return 0;131}

Fzu 2150 Fire Game (high posture bfs--two starting points)

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.