Fzu 2150-fire game-[BFS]

Source: Internet
Author: User

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.

Sample Input
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#

Sample output
Case 1: 1
Case 2:-1
Case 3: 0
Case 4: 2

 

Question:

Two people set fire to one or two points on a flat lawn. Every unit of time the fire spread to four grids around the Earth (if it was lawn). They asked how much time they could burn the entire lawn.

 

Question:

$ N, m $ is very small. You can forcibly enumerate the starting points of two people and then BFs. For each BFs, the time required to burn the lawn is returned.

 

Can pass the sample code (because fuda's OJ has collapsed ......) :

#include<bits/stdc++.h>using namespace std;const int INF=0x3f3f3f3f;const int dx[4]={1,0,-1,0};const int dy[4]={0,1,0,-1};int n,m;char mp[15][15];struct Node{    int x,y;    int step;    Node(){}    Node(int _x,int _y,int _step) {        x=_x, y=_y, step=_step;    }    bool operator==(const Node &oth)const {        return x==oth.x && y==oth.y && step==oth.step;    }};int vis[15][15];queue<Node> Q;int bfs(Node a,Node b){    memset(vis,-1,sizeof(vis));    if(a==b) Q.push(a);    else Q.push(a), Q.push(b);    vis[a.x][a.y]=vis[b.x][b.y]=0;    while(!Q.empty())    {        Node now=Q.front(); Q.pop();        for(int k=0;k<4;k++)        {            Node nxt=Node(now.x+dx[k],now.y+dy[k],now.step+1);            if(mp[nxt.x][nxt.y]==‘.‘) continue;            if(vis[nxt.x][nxt.y]!=-1) continue;            vis[nxt.x][nxt.y]=nxt.step;            Q.push(nxt);        }    }    int res=-1;    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        {            if(mp[i][j]==‘.‘) continue;            if(vis[i][j]==-1) return INF;            else res=max(res,vis[i][j]);        }    }    return res;}int main(){    int T;    cin>>T;    for(int kase=1;kase<=T;kase++)    {        scanf("%d%d",&n,&m);        for(int i=0;i<15;i++) for(int j=0;j<15;j++) mp[i][j]=‘.‘;        for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);        int ans=INF;        for(int i1=1;i1<=n;i1++)        {            for(int j1=1;j1<=m;j1++)            {                if(mp[i1][j1]==‘.‘) continue;                for(int i2=1;i2<=n;i2++)                {                    for(int j2=1;j2<=m;j2++)                    {                        if(mp[i2][j2]==‘.‘) continue;                        ans=min(ans,bfs(Node(i1,j1,0),Node(i2,j2,0)));                    }                }            }        }        if(ans>=INF) ans=-1;        printf("Case %d: %d\n",kase,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.