精选文章 FZU - 2150 Fire Game(双路bfs)

FZU - 2150 Fire Game(双路bfs)

作者:2018_XWJ 时间: 2019-11-05 12:14:09
2018_XWJ 2019-11-05 12:14:09

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

与朴素的bfs不同的是:

前一步和后一步之间不再是一对一的关系,而是一对多。

最初两个结点同时入队。

所以需要在pop时将同一步的所有后继全都pop出来。

用vector存了草垛的位置,枚举两个起点。

注意剪枝。

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int inf=0x3f3f3f3f;
int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int mp[20][20];
int be[20][20];///每次bfs时mp的副本
int n,m,tot,cnt,ans,minn;

struct node
{
    int a,b;
    node(int a,int b):a(a),b(b){}///构造函数
};

vectorvec;///存草垛的位置

void bfs(node x,node y)///两点同时入队
{
    queueq;
    q.push(x);
    q.push(y);
    cnt+=2;
    int step=0;
    be[x.a][x.b]=0;
    be[y.a][y.b]=0;
    while(q.size())
    {
        step++;///新的一层
        if(step>=ans)///剪枝
            return;
        int cn=q.size();
        while(cn--)///将此时队里的所有点,即同一层的点全部pop出来,算是一步
        {
            node tmp=q.front();
            q.pop();
            for(int i=0; i<4; i++)
            {
                int fx=tmp.a+dir[i][0];
                int fy=tmp.b+dir[i][1];
                if(fx>=1&&fx<=n&&fy>=1&&fy<=m&&be[fx][fy])
                {
                    be[fx][fy]=0;
                    q.push(node(fx,fy));
                    cnt++;///有cnt个草垛被烧过了
                    if(cnt==tot)
                    {
                        ans=step;
                        return;
                    }
                }
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int k=1; k<=t; k++)
    {
        vec.clear();
        scanf("%d%d",&n,&m);
        minn=inf;
        tot=0;
        char c;
        for(int i=1; i<=n; i++)
        {
            getchar();
            for(int j=1; j<=m; j++)
            {
                c=getchar();
                if(c=='.')
                    mp[i][j]=0;
                else if(c=='#')
                {
                    mp[i][j]=1;
                    tot++;
                    vec.push_back(node(i,j));
                }
            }
        }
//        cout<<<'\n';>2)
        {
            for(int i=0; i<<'\n';>

 

相关文章

勿删,copyright占位
分享文章到微博
分享文章到朋友圈

上一篇:代码调试指南--文末抽奖

下一篇:vue数据改变了,视图不更新不刷新问题

您可能感兴趣

华为云40多款云服务产品0元试用活动

免费套餐,马上领取!
CSDN

CSDN

中国开发者社区CSDN (Chinese Software Developer Network) 创立于1999年,致力为中国开发者提供知识传播、在线学习、职业发展等全生命周期服务。