Fzu 2150 fire game (BFS)

Source: Internet
Author: User
Fire game Time limit:1000 ms Memory limit:32768kb 64bit Io format:% I64d & % i64u

Description

Fat brother and maze are playing a kind of special (hentai) Game On an N * m Board (N rows, M columns ). at the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. firstly they choose two grids which are 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 is adjacent to this grid will 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 no new grid get fire. if then all the grid which are consisting of grass is get fired, fat brother and maze will 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 that the grass in the Board wowould never burn out and the empty grid wowould never get fire.

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

Input

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

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

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

Output

For each case, output 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.

 

This is to say that from two random dots of grass, the fire will spread to the upper left and the lower left, and the adjacent grass will be lit and asked how long to ignite all grass

First, it is very troublesome to complete two points at the same time. In fact, it is difficult to enumerate the two points at the same time for the queue to perform BFS

When the lawn is less than or equal to 2, special treatment is required.

#include<iostream>#include<cstdio>#include<cmath>#include<queue>#include<cstring>#include<stdlib.h>#include<algorithm>using namespace std;char a[15][15];int vis[15][15];int wis[15][15][15][15];int ans,sum,starx,stary,starxx,staryy,n,m,mark,all;int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};const int INF=0x3f3f3f3f;struct node{    int x,y;    int step;};node p,q;int panduan(int i,int j){    if(0<=i&&i<n && 0<=j&&j<m && a[i][j]==‘#‘)        return 1;    return 0;}void BFS(){    memset(vis,0,sizeof(vis));    queue <node> Q;    sum=-2;    vis[starx][stary]=1;    vis[starxx][staryy]=1;    p.x=starx;    p.y=stary;    p.step=0;    q.x=starxx;    q.y=staryy;    q.step=0;    Q.push(p);    Q.push(q);    while(!Q.empty())    {        node now,next;        now=Q.front();        Q.pop();        for(int i=0;i<4;i++)        {            next.x=now.x+dir[i][0];            next.y=now.y+dir[i][1];            if(panduan(next.x,next.y) && !vis[next.x][next.y])            {                vis[next.x][next.y]=1;                next.step=now.step+1;                all++;                Q.push(next);            }        }        sum=max(sum,now.step);    }}int main(){        int kase,cnt=0;    scanf("%d",&kase);    while(kase--)    {        memset(wis,0,sizeof(wis));        ans=INF,mark=0;        scanf("%d %d",&n,&m);        for(int i=0;i<n;i++)        {            scanf("%s",a[i]);            for(int j=0;j<m;j++)                if(a[i][j]==‘#‘)                    mark++;            getchar();        }          if(mark<=2)        {            printf("Case %d: 0\n",++cnt);            continue;        }        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                if(a[i][j]!=‘#‘) continue;                for(int ii=0;ii<n;ii++)                {                    for(int jj=0;jj<m;jj++)                    {                        if(panduan(i,j) && panduan(ii,jj) && (i!=ii || j!=jj) )                        {                            if(wis[i][j][ii][jj]==1) continue;                            wis[i][j][ii][jj]=wis[ii][jj][i][j]=1;                            starx=i;                            stary=j;                            starxx=ii;                            staryy=jj;                            all=2;                            BFS();                            if(ans>sum && mark==all)                                {                                    ans=sum;                                }                        }                    }                }            }        }        if(ans==INF)            printf("Case %d: -1\n",++cnt);        else            printf("Case %d: %d\n",++cnt,ans);    }    return 0;}

  

 

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.