FZU2150 Fire Game(BFS)

题解

题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的。问你最少花多少时间可以烧掉,如果烧不掉就输出-1

直接遇到草就加入队伍中,然后BFS,再暴力枚举两个草丛求最小

代码

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define MAX 100000
#define LL long long
int cas=1,T;
const int INF = 99999999 ;
int n,m;
char ch[15][15];
int cnt;
int vis[110][110];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct Node
{
    int x,y;
    int step;
}q1,q2,mapp[150];

int bfs(int x1,int y1,int x2,int y2)
{
    int maxx = 0;
    queue<Node>que;
    q1.x=x1,q1.y=y1,q1.step=0;
    q2.x=x2,q2.y=y2,q2.step=0;
    que.push(q1);
    que.push(q2);
    while (!que.empty())
    {
        struct Node st1,st=que.front();
        que.pop();
        for (int i =0;i<4;i++)
        {
            int xx = st.x+dir[i][0];
            int yy = st.y+dir[i][1];
            if (!vis[xx][yy]&& ch[xx][yy]=='#'&&(xx>=0 && xx<n&&yy>=0&&yy<m))
            {
                vis[xx][yy]=1;
                st1.x=xx;
                st1.y==yy;
                st1.step=st.step+1;
                que.push(st1);
            }
        }
        maxx=max(maxx,st.step);
    }
    return maxx;
}
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&n,&m);
        cnt=0;
        for (int j = 0;j<n;j++)
        {
            scanf("%s",ch[j]);
            for (int k = 0;k<m;k++)
                if (ch[j][k]=='#')
                {
                    cnt++;
                    mapp[cnt].x=j;
                    mapp[cnt].y=k;
                }
        }
        printf("Case %d: ",cas++);
        if (cnt<=2)
        {
            printf("0\n");
            continue;
        }
        int minn=INF;
        for (int j = 0;j<cnt;j++)
            for (int k = j;k<cnt;k++)
            {
                memset(vis,0,sizeof(vis));
                vis[mapp[j].x][mapp[j].y]=1;
                vis[mapp[k].x][mapp[k].y]=1;
                bool flag = false;
                int minnn=bfs(mapp[j].x,mapp[j].y,mapp[k].x,mapp[k].y);
                for (int h = 0;h<n;h++)
                {
                    for (int l = 0;l<m;l++)
                    {
                        if (ch[h][l]!='#')
                            continue;
                        if (!vis[h][l])
                        {
                            flag=true;
                            break;
                        }
                        if (flag)
                            break;
                    }
                    if (!flag)
                        minn=min(minn,minnn);
                }
            }
        if (minn==INF)
            printf("-1\n");
        else
            printf("%d\n",minn);
    }
    //freopen("in","r",stdin);
    //scanf("%d",&T);
    //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值