I - Fire Game //两个点的bfs FZU-2150

FZU-Problem 2150 Fire Game

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
 

题意:两个人选择网格,同时开始烧草,他们要把所有的草都烧,找出烧草所用的最短时间,如果不能就输出-1。

让两个人同时开始烧草,传两个人开始烧草的位置就可以,要找出烧草所用的最短时间,就要把两人烧草的位置都要试一试,找出最适合的烧草时间所用的最短的时间,如果每一种情况都不能把草烧干净,就输出-1;而在找烧草的时间的时候,是找的最大的时间,因为时间越大越有可能把草烧完。

起初

mark[last.x][last.y]>mark[head.x][head.y]+1

这一句不明白为什么要+1;现在我觉得实在判断现在这个点是否已经被烧过了,如果已经烧过了,(Mark数组里面存放的是烧草的时间),那么就是在找最短的烧草时间。

参考链接

AC代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
    int x;
    int y;
}S,T;
char mp[20][20];
int vis[20][20];
int mark[20][20];
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int n,m;
int bfs(node S, node T)
{
    queue<node>Q;
    node head,last;
    memset(vis,0,sizeof(vis));
    memset(mark,INF,sizeof(mark));
    vis[S.x][S.y]=1;
    vis[T.x][T.y]=1;
    mark[S.x][S.y]=0;
    mark[T.x][T.y]=0;
    Q.push(S);
    Q.push(T);
    while(!Q.empty())
    {
        head=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            last.x=head.x+dir[i][0];
            last.y=head.y+dir[i][1];
            if(last.x>=0 && last.x<n && last.y>=0 && last.y<m && mp[last.x][last.y]=='#' && vis[last.x][last.y]==0 && mark[last.x][last.y]>mark[head.x][head.y]+1)
            {
                vis[last.x][last.y]=1;
                mark[last.x][last.y]=mark[head.x][head.y]+1;
                Q.push(last);
            }
        }
    }
    int ma=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(mp[i][j]=='#')
            {
             ma=max(ma,mark[i][j]);    
     //当mark数组存放完所有的烧草时间之后,就找烧草时间中最大的烧草时间,因为烧草时间最大,
     //那么就有可能烧的越多,火蔓延的时间越长,才越有可能把所有的草都烧尽了
            }
        }
    }
    return ma;
}
int main()
{
    int t;
    int ans;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        ans=INF;
        scanf("%d%d",&n,&m);
        for(int j=0;j<n;j++)
        {
            scanf("%s",mp[j]);
        }
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<m;k++)
            {
                if(mp[j][k]=='#')
                {
                    S.x=j;
                    S.y=k;
                    for(int p=0;p<n;p++)
                    {
                        for(int q=0;q<m;q++)
                        {
                            if(mp[p][q]=='#')
                            {
                                T.x=p;
                                T.y=q;
                                ans=min(ans,bfs(S,T));//找最小的烧草时间
                            }
                        }
                    }
                }
            }
        }

        if(ans==INF)
        {
            printf("Case %d: -1\n",i);
        }
        else
        {
            printf("Case %d: %d\n",i,ans);
        }
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值