USACO2.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

The lower left-hand corner is *nine* steps from the closest exit.

题解:BFS。此题就是从两个出口分别BFS一次,然后取每个格子距两个出口的距离中较小值的集合中的最大值输出即可。

/*

ID:spcjv51

PROG:maze1

LANG:C

*/

#include<stdio.h>

#include<string.h>

#include<ctype.h>

int m,n,step;

int path[2][250][250];

int f[250][250],xx[2],yy[2];

int dx[4]={-1,0,1,0};

int dy[4]={0,1,0,-1};

int q[2][30000];

int visit[250][250];

void BFS(int x,int y,int r)

{

    int head,tail,xx,yy,i;

    memset(visit,0,sizeof(visit));

    head=0; tail=1;

    visit[x][y]=1;

    q[0][1]=x; q[1][1]=y;

    path[r][x][y]=2;

    while(head<tail)

    {

        head++;

        for(i=0;i<4;i++)

        {

           xx=q[0][head]+dx[i];

           yy=q[1][head]+dy[i];

           if(visit[xx][yy]==0&&f[xx][yy])

           {

               tail++;

               q[0][tail]=xx;

               q[1][tail]=yy;

               visit[xx][yy]=1;

               path[r][xx][yy]=path[r][q[0][head]][q[1][head]]+1;

           }



        }

    }



}



int main(void)

{

    freopen("maze1.in","r",stdin);

    freopen("maze1.out","w",stdout);

    int i,j,maxn,min,l;

    char ch;

    scanf("%d%d",&n,&m);

    maxn=-1;

    memset(f,0,sizeof(f));

    memset(path,0,sizeof(path));

    l=-1;

    for(i=1; i<=2*m+1; i++)

    {

        getchar();

        for(j=1; j<=2*n+1; j++)

        {

            scanf("%c",&ch);

            if(ch==' ')

                f[i][j]=1;

            else

                f[i][j]=0;

        }

    }



    for(i=1; i<=2*m+1; i++)

        for(j=1; j<=2*n+1; j++)

        {



            if((i==1||i==2*m+1)&&f[i][j]==1)

            {

                l++;

                if(i==1)

                    xx[l]=i+1;

                else

                    xx[l]=i-1;

                yy[l]=j;

            }

            if((j==1||j==2*n+1)&&i>1&&i<2*m+1&&f[i][j]==1)

            {



                l++;

                xx[l]=i;

                if(j==1)

                    yy[l]=j+1;

                else

                    yy[l]=j-1;



            }

        }

    BFS(xx[0],yy[0],0);

    BFS(xx[1],yy[1],1);

    for(i=1; i<=2*m+1; i++)

        for(j=1; j<=2*n+1; j++)

        {

            min=path[0][i][j]<path[1][i][j]?path[0][i][j]:path[1][i][j];

            if(min>maxn) maxn=min;

        }

    printf("%d\n",maxn/2);

    return 0;

}

 

你可能感兴趣的:(USACO)