zoukankan      html  css  js  c++  java
  • 2.4.2 Overfencing

    Overfencing
    Kolstad and Schrijvers

    Farmer John went crazy and created a huge maze of fences out in a field. Happily, he left out two fence segments on the edges, and thus created two "exits" for the maze. Even more happily, the maze he created by this overfencing experience is a `perfect' maze: you can find a way out of the maze from any point inside it.

    Given W (1 <= W <= 38), the width of the maze; H (1 <= H <= 100), the height of the maze; 2*H+1 lines with width 2*W+1 characters that represent the maze in a format like that shown later - then calculate the number of steps required to exit the maze from the `worst' point in the maze (the point that is `farther' from either exit even when walking optimally to the closest exit). Of course, cows walk only parallel or perpendicular to the x-y axes; they do not walk on a diagonal. Each move to a new square counts as a single unit of distance (including the move "out" of the maze.

    Here's what one particular W=5, H=3 maze looks like:

    +-+-+-+-+-+
    |         |
    +-+ +-+ + +
    |     | | |
    + +-+-+ + +
    | |     |  
    +-+ +-+-+-+
    

    Fenceposts appear only in odd numbered rows and and odd numbered columns (as in the example). The format should be obvious and self explanatory. Each maze has exactly two blank walls on the outside for exiting.

    PROGRAM NAME: maze1

    INPUT FORMAT

    Line 1: W and H, space separated
    Lines 2 through 2*H+2: 2*W+1 characters that represent the maze

    SAMPLE INPUT (file maze1.in)

    5 3
    +-+-+-+-+-+
    |         |
    +-+ +-+ + +
    |     | | |
    + +-+-+ + +
    | |     |  
    +-+ +-+-+-+
    

    OUTPUT FORMAT

    A single integer on a single output line. The integer specifies the minimal number of steps that guarantee a cow can exit the maze from any possible point inside the maze.

    SAMPLE OUTPUT (file maze1.out)

    9
    
    /*
    ID: makeeca1
    PROG: maze1
    LANG: C++
    */
    #include<iostream>
    #include<cstdio>
    using namespace std;
    int w,h,xmin[210][100],dl[20000][2],tail=0,fx[4][2]={{-2,0},{0,2},{2,0},{0,-2}},fy[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
    char map[210][100];
    void  bfs(int head)
    {
        int total=0,x,y,p,q;
        xmin[dl[1][0]][dl[1][1]]=1;
        xmin[dl[2][0]][dl[2][1]]=1;
        if((dl[tail][1]==dl[tail-1][1])&&(dl[tail-1][0]==dl[tail][0])) tail--;
        while(head<=tail)
        {
            head++;
            x=dl[head][0];
            y=dl[head][1];
            for(int i=0;i<=3;i++)
            {
                p=x+fx[i][0];q=y+fx[i][1];
                if((p>0)&&(p<h)&&(q>0)&&(q<w)&&(map[x+fy[i][0]][y+fy[i][1]]==' ')) 
                if((xmin[p][q]==0)||(xmin[p][q]>(xmin[x][y]+1))) 
                {
                    tail++;
                    dl[tail][0]=p;
                    dl[tail][1]=q;
                    xmin[p][q]=xmin[x][y]+1;
                }
            }
        }
        for(int i=1;i<=h;i++) 
            for(int j=1;j<=w;j++)
                total=(xmin[i][j]>total?xmin[i][j]:total);
        printf("%d
    ",total);
    }
    int main()
    {
        freopen("maze1.in","r",stdin);
        freopen("maze1.out","w",stdout);
        scanf("%d%d",&w,&h);
        h=2*h+1;
        w=2*w+1;
        for(int i=1;i<=h;i++)
        {
            getchar();
            for(int j=1;j<=w;j++)
            {
                map[i][j]=getchar();
                if (map[i][j]==' '){
                    if( i==1 ) {dl[++tail][0]=i+1;dl[tail][1]=j;}
                    if( i==h ) {dl[++tail][0]=i-1;dl[tail][1]=j;}
                    if( j==1 ) {dl[++tail][0]=i;dl[tail][1]=j+1;}
                    if( j==w ) {dl[++tail][0]=i;dl[tail][1]=j-1;}
                }
            }
        }
        bfs(0);
        return 0;
    }
  • 相关阅读:
    Macos同时配置github与公司内部使用的gitlab
    position跟display、margin collapse、overflow、float这些特性相互叠加后会怎么样?
    Echarts实现嵌套双环饼状图
    MySQL 8.0.19安装教程(windows 64位)
    解决ECharts Can't get dom width or height!无法初始化图表的问题
    maven项目的pom.xml提示Missing artifact traffic-iso.sdk:sdk:jar:0.0.1
    ES6数组去重的常用方法
    uni-app中设置全局变量和动态修改全局变量
    彻底搞懂$router 和 $route
    onselectstart
  • 原文地址:https://www.cnblogs.com/makeecat/p/3274623.html
Copyright © 2011-2022 走看看