USACO Section 2.4 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。

View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: maze1
*/
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#define MAXN 105
#define MAXM 40
#define INF 10000

using namespace std;

int dist[2*MAXN][2*MAXM],n,m;
char map[2*MAXN][2*MAXM];
bool visited[2*MAXN][2*MAXM];
struct point{int x,y,z;}s[2];

void BFS(point s)
{
    point t,tt;
    memset(visited,false,sizeof(visited));
    queue<point>Q;
    Q.push(s);
    visited[s.x][s.y]=true;
    while(!Q.empty())
    {
        t=Q.front();Q.pop();
        dist[t.x][t.y]=min(dist[t.x][t.y],t.z);
        tt.z=t.z+1;
        if(t.x<2*n&&map[t.x+1][t.y]==' '&&!visited[t.x+2][t.y])
        {
            tt.x=t.x+2;tt.y=t.y;
            Q.push(tt);
            visited[tt.x][tt.y]=true;
        }
        if(t.x>2&&map[t.x-1][t.y]==' '&&!visited[t.x-2][t.y])
        {
            tt.x=t.x-2;tt.y=t.y;
            Q.push(tt);
            visited[tt.x][tt.y]=true;
        }
        if(t.y<2*m&&map[t.x][t.y+1]==' '&&!visited[t.x][t.y+2])
        {
            tt.x=t.x;tt.y=t.y+2;
            Q.push(tt);
            visited[tt.x][tt.y]=true;
        }
        if(t.y>2&&map[t.x][t.y-1]==' '&&!visited[t.x][t.y-2])
        {
            tt.x=t.x;tt.y=t.y-2;
            Q.push(tt);
            visited[tt.x][tt.y]=true;
        }
    }
}

int main()
{
    freopen("maze1.in","r",stdin);
    freopen("maze1.out","w",stdout);
    int i,j;
    while(scanf("%d%d",&m,&n)==2)
    {
        getchar();
        for(i=1;i<=2*n+1;i++)
        {
            for(j=1;j<=2*m+1;j++)
                scanf("%c",&map[i][j]);
            getchar();
        }
        j=0;
        for(i=2;i<=2*n+1;i+=2)
        {
            if(map[i][1]==' ')
            {
                s[j].x=i;s[j].y=2;s[j].z=1;
                j++;
            }
            if(map[i][2*m+1]==' ')
            {
                s[j].x=i;s[j].y=2*m;s[j].z=1;
                j++;
            }
        }
        for(i=2;i<=2*m+1;i+=2)
        {
            if(map[1][i]==' ')
            {
                s[j].x=2;s[j].y=i;s[j].z=1;
                j++;
            }
            if(map[2*n+1][i]==' ')
            {
                s[j].x=2*n;s[j].y=i;s[j].z=1;
                j++;
            }
        }
        for(i=2;i<2*n+1;i+=2)
        {
            for(j=2;j<2*m+1;j+=2)
            {
                dist[i][j]=INF;
            }
        }
        BFS(s[0]);
        BFS(s[1]);
        int ans=0;
        for(i=2;i<2*n+1;i++)
        {
            for(j=2;j<2*m+1;j++)
            {
                if(ans<dist[i][j])
                    ans=dist[i][j];
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

 

posted @ 2012-09-06 10:10  mtry  阅读(328)  评论(0编辑  收藏  举报