usaco 2.4 maze1

从两个出口开始dfs搜索,记录到达每一个格子的最小值。

最后输出整个地图步数的最大值

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

char m[202][101];
int t[202][101];
int x[2],y[2],ct;
int w,h;

int walk[][2] = {{-1,0},
                {0,+1},
                {+1,0},
                {0,-1},
                };



bool check_do(int x,int y)
{
    if (m[x][y]!='-'&&m[x][y]!='|')
        return true;
    else return false;
}

void do_search(int x,int y)
{
    int next_x,next_y;
    for (int i=0;i<4;i++)
    {
        next_x = x + walk[i][0];
        next_y = y + walk[i][1];
        if (check_do(next_x,next_y))
        {
            next_x += walk[i][0];
            next_y += walk[i][1];
            if (next_x>=0&&
                next_x<=2*h&&
                next_y>=0&&
                next_y<=2*w&&
                (t[next_x][next_y]==0||t[x][y]+1<t[next_x][next_y])
                )
            {
                t[next_x][next_y] = t[x][y]+1;
                do_search(next_x,next_y);
            }
        }
    }
}

int main()
{
    freopen("maze1.in","r",stdin);
    freopen("maze1.out","w",stdout);
    scanf("%d %d",&w,&h);
    for (int i=0;i<=2*h;i++)
    {
        getchar();
        for (int j=0;j<=2*w;j++)
        {
            scanf("%c",&m[i][j]);
        }
    }
    ct = 0;
    for (int i=0;i<=2*h;i++)
    {
        if (m[i][0]==' ')
        {
            x[ct] = i;
            y[ct] = 1;
            ct++;
        }
        if (m[i][2*w]==' ')
        {
            x[ct] = i;
            y[ct] = 2*w-1;
            ct++;
        }
    }
    for (int j=0;j<=2*w;j++)
    {
        if (m[0][j]==' ')
        {
            x[ct] = 1;
            y[ct] = j;
            ct++;
        }
        if (m[2*h][j]==' ')
        {
            x[ct] = 2*h-1;
            y[ct] = j;
            ct++;
        }
    }
//    printf("%d %d\n%d %d",x[0],y[0],x[1],y[1]);
    memset(t,0,sizeof(t));
    t[x[0]][y[0]] = 1;
    t[x[1]][y[1]] = 1;
    do_search(x[0],y[0]);
    do_search(x[1],y[1]);
    int maxi = 0;
    for (int i=0;i<=2*h;i++)
    {
        for (int j=0;j<=2*w;j++)
        {
            if (t[i][j]>maxi)
                maxi = t[i][j];
        }
    }
    printf("%d\n",maxi);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值