Fzu Problem 2150 Fire Game (double start bfs AH)

Source: Internet
Author: User

Title Link: http://acm.fzu.edu.cn/problem.php?pid=2150


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

Test instructions

a graph of M * N, '. ' Show open space,' # ' indicates the lawn, can choose to be in any of the two lawn lattice as the starting point for ignition, every 1 s flame will spread to the surrounding four lattice,

Choosing which two points can make the burning of all lawns take the least time.

The code is as follows:

#include <cstdio> #include <cstring> #include <vector> #include <queue> #include <iostream > #include <algorithm>using namespace std; #define INF 0x3f3f3f3fconst int maxn = 17;int Dx[4] = {0, 0, 1, -1};int    Dy[4] = {1,-1, 0, 0};struct node{int x, y; int C;};    struct pp{int x, y; int C;} Pp[maxn];int N, M;char mm[maxn][maxn];int vis[maxn][maxn];queue<node > Q;vector<node > V;int BFS (Node A,node    b) {node p;    memset (vis,0,sizeof (VIS));    A.C = 0;    B.C = 0;    VIS[A.X][A.Y] = 1;    VIS[B.X][B.Y] = 1;    Q.push (a);    Q.push (b);    int k;        while (!q.empty ()) {p = Q.front ();        Q.pop ();        K = p.c;            for (int i = 0; i < 4; i++) {b.x = p.x + dx[i];            B.Y = p.y + dy[i];            B.C = p.c + 1; if (b.x>=1 && b.x<=n && b.y>=1 && b.y<=m && mm[b.x][b.y]== ' # ' && vis[ b.x][b.y]==0) {VIS[B.X][B.Y] = 1;            Q.push (b); }}} return k;}  int judge () {for (int k = 1, k <= N; k++) {for (int l = 1; l <= m; l++) {if (Vis[k][l]            = = 0 && mm[k][l]== ' # ')//not burnt out {return 1; }}} return 0;}    int main () {int t;    int cas = 0;    int step;    scanf ("%d", &t);        while (t--) {while (!q.empty ()) {Q.pop ();        } scanf ("%d%d", &n,&m);        V.clear ();        int l = 0;            for (int i=1; i<=n; i++) {GetChar ();                for (int j=1; j<=m; J + +) {scanf ("%c", &mm[i][j]);                    if (mm[i][j] = = ' # ') {pp[l].x = i;                    Pp[l].y = j;                    pp[l].c = 0;                    V.push_back (Pp[l]);      V.push_back (node) {i,j,0});          }}} step = INF;        int len = V.size (); for (int i = 0, i < len; i++) {for (int j = i; J < Len; J + +) {int tt = b                FS (V[i], v[j]);                if (!judge ()) {if (Step > TT) step = TT;        }}} printf ("Case%d:", ++cas);            if (step = = INF) {printf (" -1\n");        Continue    } printf ("%d\n", step); } return 0;}



Fzu Problem 2150 Fire Game (double start bfs AH)

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.