Fire Game FZU - 2150

#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
char maze[11][11];
int book[11][11];
struct point {
    int x,y;
    int time;
};
vector<point>vec;
queue<point>que;
int go[4][2]={ {1,0},{0,1},{-1,0},{0,-1}};
int main()
{
    int num;
    scanf("%d",&num);

    for(int k=1;k<=num;k++)
    {
        int a,b;
        int flag=0;
        scanf("%d %d",&a,&b);
        vec.clear();
        int count=0,mintime=999999,cnt=0;
        point now;
        for(int i=0;i<a;i++)
        {
            scanf("%s",maze[i]);
            for(int j=0;j<b;j++)
            {
                if(maze[i][j]=='#')
                {
                    now.y=i;
                    now.x=j;
                    now.time=0;
                    vec.push_back(now);
                    count++;
                }

            }
        }
        if(count==1)
        {
            printf("Case %d: 0\n",k);
            continue;
        }

        for(int i=0;i<vec.size();i++)
        {
            for(int j=i+1;j<vec.size();j++)
            {
                cnt=0;
                memset(book,0,sizeof(book));
                que.push(vec[i]);
                book[vec[i].y][vec[i].x]=1;
                que.push(vec[j]);
                book[vec[j].y][vec[j].x]=1;
                while(!que.empty())
                {
                    now=que.front();
                    que.pop();
                    cnt++;
                    if(cnt==count)
                    {
                        flag=1;
                        mintime=min(now.time,mintime);
                        break;
                    }
                    now.time++;
                    for(int i=0;i<4;i++)
                    {
                        point p=now;
                        p.x+=go[i][0];
                        p.y+=go[i][1];
                        if(p.x>=0&&p.y>=0&&p.y<a&&p.x<b&&!book[p.y][p.x]&&maze[p.y][p.x]=='#')
                        {

                            book[p.y][p.x]=1;
                            que.push(p);
                        }
                    }
                }
                    while(!que.empty())
                    que.pop();
            }

        }
        while(!que.empty())
        que.pop();
        if(flag)
        printf("Case %d: %d\n",k,mintime);
        else
        printf("Case %d: -1\n",k);

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值