FZU - 2150 Fire Game(双路bfs)

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 <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#include <vector>
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){}///构造函数
};

vector<node>vec;///存草垛的位置

void bfs(node x,node y)///两点同时入队
{
    queue<node>q;
    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<<tot<<'\n';
        if(tot>2)
        {
            for(int i=0; i<tot; i++)
            {
                for(int j=i; j<tot; j++)
                {
//                    cout<<vec[i].a<<' '<<vec[i].b<<'\n';
//                    cout<<vec[j].a<<' '<<vec[j].b<<'\n';
                    memcpy(be,mp,sizeof(mp));
                    cnt=0;
                    ans=inf;
                    bfs(vec[i],vec[j]);
                    minn=min(minn,ans);
                }
            }
            if(minn==inf)
                minn=-1;
        }
        else
            minn=0;
        cout<<"Case "<<k<<": "<<minn<<'\n';
    }
    return 0;
}

 

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
5015次浏览 47人参与
# 你的实习产出是真实的还是包装的? #
1103次浏览 27人参与
# 巨人网络春招 #
11175次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
6907次浏览 37人参与
# 简历第一个项目做什么 #
31251次浏览 312人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186349次浏览 1115人参与
# MiniMax求职进展汇总 #
22892次浏览 293人参与
# 面试紧张时你会有什么表现? #
30371次浏览 188人参与
# 简历中的项目经历要怎么写? #
309379次浏览 4152人参与
# 网易游戏笔试 #
6304次浏览 83人参与
# 职能管理面试记录 #
10687次浏览 59人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6850次浏览 154人参与
# 从哪些方向判断这个offer值不值得去? #
56698次浏览 357人参与
# 腾讯音乐求职进展汇总 #
160394次浏览 1105人参与
# 小红书求职进展汇总 #
226845次浏览 1356人参与
# AI时代,哪些岗位最容易被淘汰 #
62406次浏览 728人参与
# 你怎么看待AI面试 #
179273次浏览 1164人参与
# 正在春招的你,也参与了去年秋招吗? #
362529次浏览 2631人参与
# 你的房租占工资的比例是多少? #
92123次浏览 896人参与
# 机械求职避坑tips #
94396次浏览 567人参与
# 校招笔试 #
466318次浏览 2950人参与
# 面试官最爱问的 AI 问题是...... #
27111次浏览 834人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务