1. 程式人生 > >【FZU2150】Fire Game(兩起點bfs)

【FZU2150】Fire Game(兩起點bfs)

題目連結

Problem 2150 Fire Game

Accept: 3827    Submit: 13039 Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem 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 would never burn out and the empty grid would 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

【題意】

兩個小朋友玩火,在一個地圖上#表示乾草,開始可以點燃兩個乾草(可重疊),火每秒可向周圍四個方向蔓延,空地處不能過,問所有的乾草是否能燃盡,如能,輸出最快時間。

【解題思路】

與其他題不同的是本題的bfs是雙起點的,但是也沒啥不同,先將“#”離散出來,然後列舉每兩個“#”的座標,最後只需判斷每個點是否被遍歷過即可,如果都被遍歷過說明這種方法是可行的,如果沒有則返回INF,即不更新ans。

【程式碼】

#include<cstdio>
#include<set>
#include<cstring>
#include<queue>
using namespace std;
int disx[]={1,0,0,-1};
int disy[]={0,1,-1,0};
const int maxn=15;
const int INF=0x3f3f3f3f;
char s[maxn][maxn];
int cnt,vis[maxn][maxn],n,m;
struct Node
{
    int x,y,step;
}node[105];
int judge()
{
    for(int i=0;i<cnt;i++)
    {
        if(!vis[node[i].x][node[i].y])
            return 0;
    }
    return 1;
}
int bfs(int x,int y)
{
    queue<Node>q;
    int step=0;
    q.push(node[x]);q.push(node[y]);
    memset(vis,0,sizeof(vis));
    vis[node[x].x][node[x].y]=1;
    vis[node[y].x][node[y].y]=1;
    while(!q.empty())
    {
        Node t=q.front(),p;
        q.pop();
        for(int i=0;i<4;i++)
        {
            p.x=t.x+disx[i];
            p.y=t.y+disy[i];
            p.step=t.step+1;
            if(p.x>=0 && p.x<n && p.y>=0 && p.y<m && s[p.x][p.y]=='#' && !vis[p.x][p.y])
            {
                vis[p.x][p.y]=1;
                step=max(step,p.step);
                q.push(p);
            }
        }
    }
    if(judge())return step;
    else return INF;
}
int main()
{
    int T,kase=1;
    scanf("%d",&T);
    while(T--)
    {
        cnt=0;
        int ans=INF;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(s[i][j]=='#')
                {
                    node[cnt].x=i;
                    node[cnt].y=j;
                    node[cnt++].step=0;
                }
            }
        }
        for(int i=0;i<cnt;i++)
            for(int j=i;j<cnt;j++)
                ans=min(ans,bfs(i,j));
        if(ans!=INF)printf("Case %d: %d\n",kase++,ans);
        else printf("Case %d: -1\n",kase++);
    }
    return 0;
}