USACO-Overfencing

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 isfarther’ 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以两个出口为起点分别走一遍图,找出每个点到两个出口最短的一个,然后再找出最短距离最长的一个。

代码:

/*
ID:iam666
PROG:maze1
LANG:C++
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef pair <int,int> point;
const int maxn=500;
int dx[4]={-1,0,0,1};
int dy[4]={0,-1,1,0};
int n,m;
int maze[maxn][maxn],mark[maxn][maxn];
queue <point> q;
void bfs(int x1,int y1,int x2,int y2)
{
    memset(mark,-1,sizeof(mark));
    mark[x1][y1]=mark[x2][y2]=0;
    q.push(make_pair(x1,y1));
    q.push(make_pair(x2,y2));
    while(!q.empty())
    {
        point head=q.front();
        q.pop();
        int ux=head.first,uy=head.second;
        for(int i=0;i<4;i++)
        {
            int vx=ux+dx[i];
            int vy=uy+dy[i];
            if(vx<0||vx>=n||vy<0||vy>=m||maze[vx][vy])
                continue;
            if(mark[vx][vy]==-1||mark[vx][vy]>mark[ux][uy])
                {
                    q.push(make_pair(vx,vy));
                    if(vx%2==0||vy%2==0)
                        mark[vx][vy]=mark[ux][uy];
                    else
                        mark[vx][vy]=mark[ux][uy]+1;
                }

        }
    }
}

int main (void)
{
    freopen("maze1.in","r",stdin);
    freopen("maze1.out","w",stdout);
    cin>>m>>n;
    n=n*2+1;
    m=m*2+1;
    int x1=-1,y1=-1,x2,y2;
    for(int i=0;i<n;i++)
    {
        getchar();
        for(int j=0;j<m;j++)
        {
            char ch;
            scanf("%c",&ch);
            if(ch==' ')
                maze[i][j]=0;
            else
                maze[i][j]=1;
            if(maze[i][j]==0&&(i==0||j==0||i==n-1||j==m-1))
            {
                if(x1==-1)
                {
                    x1=i;y1=j;
                }
                else
                {
                    x2=i;y2=j;
                }
            }
        }
    }
    bfs(x1,y1,x2,y2);
    int ans=0;
    int xx,yy;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            ans=max(ans,mark[i][j]);
            if(ans==mark[i][j])
            {
                xx=i,yy=j;
            }
        }
    }
    //printf("i=%d,j=%d\n",xx,yy);
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值