kuangbin 简单搜索 Fire Game FZU - 2150

Fire Game FZU - 2150

题目链接
因为 t=100,m<=10,n<=10;
所以可以枚举所有情况(2个七点,可以相同)来比较选出最小值;
凯斯因为数组开的过大 mp[500][500];因为初始化1001010次,所以初始化5001010)500500次,tle,改成mp【15】【15】就过了。
ac代码:

#include<stdio.h>
#include<string.h>
struct node{
int x;
int y;
int step;
}ll[5000],LL[5000];
int n,m,ans=0,h=0;
char mp[15][15];
int step[15][15]={0};
int chack(int x,int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]=='#'&&step[x][y]==0)
        return 1;
    return 0;
}
int a[4]={1,-1,0,0},b[4]={0,0,1,-1},x,T;
void bfs()
{
    int s=0;
    int minn=123456789,flag=0;
    memset(LL,0,sizeof(LL));
    for(int i=1;i<=h;i++)
    {
        for(int j=1;j<=h;j++)
        {
            memset(LL,0,sizeof(LL));
            memset(step,0,sizeof(step));
            LL[1].x=ll[i].x;
            LL[1].y=ll[i].y;
            LL[1].step=1;
            LL[2].x=ll[j].x;
            LL[2].y=ll[j].y;
            LL[2].step=1;
            step[LL[2].x][LL[2].y]=1;
            step[LL[1].x][LL[1].y]=1;
            int head=1;
            int tail=2;
            if(LL[1].x==LL[2].x&&LL[1].y==LL[2].y)
                s=1;
            else
                s=2;
            while(head<=tail)
            {
                for(int k=0;k<4;k++)
                {
                    int lx=LL[head].x+a[k];
                    int ly=LL[head].y+b[k];
                    if(chack(lx,ly))
                    {
                        //printf("%d %d %d\n",head,lx,ly);
                        tail++;
                        LL[tail].x=lx;
                        LL[tail].y=ly;
                        LL[tail].step=LL[head].step+1;
                        step[lx][ly]=LL[tail].step;
                        s++;
                    }
                }
                head++;
            }
            if(s==ans)
            {
                flag=1;
                if(LL[tail].step<minn)
                    minn=LL[tail].step;
            }
        }
    }
    if(flag==1)
        printf("Case %d: %d\n",x-T,minn-1);
    else
        printf("Case %d: -1\n",x-T);
}
int main()
{
    scanf("%d",&T);
    x=T;
    while(T--)
    {
        ans=0,h=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            getchar();
            for(int j=1;j<=m;j++)
            {
                scanf("%c",&mp[i][j]);
                if(mp[i][j]=='#')
                {
                    ans++;
                    ll[++h].x=i;
                    ll[h].y=j;
                    ll[h].step=0;
                }
            }
        }
        bfs();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值