广搜优化

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

题意:两个人玩游戏,两个人在n*m的方格内分别找两个有草的位置点火烧草,#是草,点燃后会向四周蔓延,问所有草都烧完最短需要多少时间。

思路:找到所有草的位置存到结构体数组中,记录草的总数,因为每次从两个位置开始点火,利用暴力把两个点的匹配的所有情况都遍历一遍,每一次都进行一次广搜,因为火从两点同时点燃,所以对两个点进行广搜,找到每一次点火烧草所需要的时间,从中筛选出最短时间,如果无论从哪两个地方点火都无法把草烧完,则让时间为极大值,从而判断最终是否能把草烧完。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int M=1010;
const int N=50;
char mp[N][N];///邻接矩阵画图
int book[N][N];///标记找到的满足条件的点
int inf=0x3f3f3f3f;
int k;///记录有多少个草(#)
int n,m;
int nx[4][2]= {0,1,1,0,0,-1,-1,0};
struct node
{
    int x;
    int y;
    int t;
} qq[M];
int bfs(node tx,node ty)
{
    int i,j,l,s=0,f=0,maxx=-inf;
    queue<node> q;///定义队列
    struct node u,v,r;///定义结构体变量
    memset(book,0,sizeof(book));///标记数组清零
    tx.t=0;
    ty.t=0;///初始时间为零
    ///标记初始位置
    book[tx.x][tx.y]=1;
    book[ty.x][ty.y]=1;
    ///初始状态入队列
    q.push(tx);
    q.push(ty);
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        f=0;///标记四周是否有草
        s++;///草(#)的数量
        for(i=0; i<4; i++)
        {
            v.x=u.x+nx[i][0];
            v.y=u.y+nx[i][1];
            if(v.x>0&&v.x<=n&&v.y>0&&v.y<=m&&book[v.x][v.y]==0&&mp[v.x][v.y]=='#')
            {
                book[v.x][v.y]=1;///标记烧过的草
                v.t=u.t+1;///时间+1
                q.push(v);///放入队列
                f=1;///标记上一个点的四周有草
            }
        }
        ///如果上一个点的四周没有草
        ///(1)全部的草都点燃
        ///(2)这个点无法把全部的草点燃
        if(f==0)
        {
            maxx=max(maxx,u.t);///记录烧到此时所花费的时间
        }
    }
    ///草未烧完
    if(s<k)
        maxx=inf;///返回极大值
    return maxx;
}
int main()
{
    int tt,sum=0;
    cin>>tt;
    while(tt--)
    {
        sum++;
        int i,j,l,s=0,minn=inf;
        k=0;
        cin>>n>>m;///n行m列
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=m; j++)
            {
                cin>>mp[i][j];
                ///找到草,记录草的位置,数组下标为草的数量
                if(mp[i][j]=='#')
                {
                    qq[k].x=i;
                    qq[k++].y=j;
                }
            }
        }
        ///暴力遍历所有情况
        for(i=0; i<k; i++)
            for(j=0; j<k; j++)
                minn=min(minn,bfs(qq[i],qq[j]));///记录烧完草的最短时间
        if(minn==inf)///说明无法烧完草
            printf("Case %d: -1\n",sum);
        else
            printf("Case %d: %d\n",sum,minn);

    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值