[kuangbin]专题一 简单搜索 Fire Game FZU - 2150【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.
胖哥哥和迷宫正在N*M板上玩一种特殊的(Hentai)游戏(N行,M列)。在开始的时候,这个板的每一个格子都是由草组成的,或者是空的,然后他们开始点燃所有的草。首先,他们分别选择两个由草组成的格子。众所周知,火可以在草中蔓延。如果格子(x,y)在时间t处着火,则与此格相邻的格子(x+1,y)、(x-1,y)、(x,y+1)、(x,y+1)将在时间t+1时着火。当没有新的格子被着火时,这个过程就会结束。如果所有由草组成的栅格都被烧掉,胖哥哥和麦子就会站在栅格的中间,玩一个更特别的(Hentai)游戏。你可以假设格子中的草永远不会被烧完,并且空格不会着火。

【输入】
The first line of the data 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
测试数据的第一行是一个整数T,表示测试数据的数目。
然后是T个样例,每个样例包含两个整数N和M,表示板的大小。然后是N行,每一行用M个字符表示板。“#”表示草,你可以假设至少有一个格子是由草地组成的。
1 <= T <=100, 1 <= n <=10, 1 <= m <=10

【输出】
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.
对于每一种情况,先输出案例号,如果他们能玩更特殊的(Hentai)游戏(把所有的草都烧掉),输出他们在放火后所需的最少时间,否则只需输出-1。有关详细信息,请参阅示例输入和输出。

【样例输入】
4
3 3
.#.

.#.
3 3
.#.
#.#
.#.
3 3

#.#

3 3

…#
#.#

【样例输出】
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

题目链接:https://cn.vjudge.net/problem/FZU-2150

穷举两个放火点进行bfs

代码如下:

#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
static const int MAXN=10;
static const int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
int n,m,ans,num;
char mp[MAXN+5][MAXN+5];
bool vis[MAXN+5][MAXN+5];
struct Node{
    int x,y;
    int step;
};
void bfs(Node point1,Node point2)
{
    queue<Node> Q;
    int maxstep=0,cnt=0;
    memset(vis,false,sizeof(vis));
    Q.push(point1);
    vis[point1.x][point1.y]=1;
    cnt++;
    Q.push(point2);
    vis[point2.x][point2.y]=1;
    cnt++;
    while(!Q.empty())
    {
        Node now=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            Node next;
            next.x=now.x+dx[i];
            next.y=now.y+dy[i];
            if(1<=next.x && next.x<=n && 1<=next.y && next.y<=m && mp[next.x][next.y]=='#' && !vis[next.x][next.y])
            {
                vis[next.x][next.y]=1;
                cnt++;
                next.step=now.step+1;
                maxstep=max(maxstep,next.step);
                Q.push(next);
            }
        }
    }
    if(cnt>=num)
        ans=min(ans,maxstep);
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    for(int kase=1;kase<=T;kase++)
    {
        num=0;
        ans=0x7fffffff;
        cout<<"Case "<<kase<<": ";
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cin>>mp[i][j];
                if(mp[i][j]=='#')
                    num++;
            }
        for(int i1=1;i1<=n;i1++)
            for(int j1=1;j1<=m;j1++)
            {
                if(mp[i1][j1]!='#') continue;
                for(int i2=i1;i2<=n;i2++)
                    for(int j2=1;j2<=m;j2++)
                    {
                        if(i2==i1 && j2<j1) continue;
                        if(mp[i2][j2]!='#') continue;
                        Node t1,t2;
                        t1.x=i1;t1.y=j1;t1.step=0;
                        t2.x=i2;t2.y=j2;t2.step=0;
                        bfs(t1,t2);
                    }
            }
        if(ans==0x7fffffff)
            cout<<-1<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值