Fzu 2150 Fire Game BFS

Source: Internet
Author: User

Fire GameTime limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U Fzu 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

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

Sample Output

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

The first team match was abused and the great god Power still has a big gap to work on.
The problem was not made at the time of the game. At first, I thought, could Dfs


Test instructions: #号是草 and two fire on the grass to burn the fire up and down to spread the spread of every time the cost of 1s ask how long it takes to burn all the grass
Two fires together BFS

After the game to see the code strength of seniors imitate a wave


#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < queue> #include <vector> #include <iomanip> #include <math.h> #include <map>using namespace STD; #define FIN freopen ("Input.txt", "R", stdin), #define FOUT freopen ("Output.txt", "w", stdout); #define INF 0x3f3f 3f3f#define infll 0x3f3f3f3f3f3f3f#define Lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef Long Long LL ; typedef pair<int,int> Pii;int N,m;char mp[15][15];int vis[15][15];int dx[4]={0,0,1,-1};int dy[4]={1,-1,0,0};   struct node{int x, y;   int time;      Node (int a,int b,int c) {x=a;      Y=b;   Time=c; }};int check (int x,int y) {return x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]& &mp[x][y]== ' # ';}   int BFS (int x,int y,int x2,int y2,int& tot) {queue<node>q;   Q.push (Node (x,y,0)); if (x!=x2| |   Y!=Y2) Q.push (node (x2,y2,0));   Vis[x][y]=1;   Vis[x2][y2]=1; int Time=0;       while (!q.empty ()) {node Fro=q.front ();       Q.pop ();       tot++;           for (int i=0;i<4;i++) {int xx=fro.x+dx[i];           int yy=fro.y+dy[i];               if (check (xx,yy)) {vis[xx][yy]=1;               Q.push (Node (xx,yy,fro.time+1));           Time=max (time,fro.time+1); }}} return time;    int main () {//fin int T;    int Cas=1;    scanf ("%d", &t);        while (t--) {int tot=0;        scanf ("%d%d", &n,&m);            for (int i=0;i<n;i++) {scanf ("%s", Mp[i]);        for (int j=0;j<m;j++) if (mp[i][j]== ' # ') tot++;        } int ans=inf;            for (int i=0;i<n*m;i++) {int x=i/m, y=i%m;  if (mp[x][y]== '. ')            Continue                for (int j=i;j<n*m;j++) {int x2=j/m, y2=j%m;  if (mp[x2][y2]== '. ')                Continue                int t=0;                memset (vis,0,sizeof (VIS));                int Now=bfs (X,Y,X2,Y2,T);  if (T!=tot)Continue            Ans=min (Ans,now);    }} printf ("Case%d:%d\n", Cas++,ans==inf -1:ans); }}

  




Fzu 2150 Fire Game BFS

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.