Overfencing(BFS)

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. 

 

     题意:

     给出 W(1 ~ 38),H(1 ~ 100),代表有 2 * W + 1 列,2 * H + 1 行的一个地图。根据这个地图,输出任意一点至少要逃出迷宫的步数,如样例输出9,就是最左下角的格子逃出去至少需要9步。

 

     思路:

     BFS。要求任意一点都必须逃离迷宫,而且需要最少的步数,那么挑选离出口最远的点BFS即可求出最少步数。因为不知道哪一点最远,故每一格都BFS一次,求出每个点的最小值,同时比较大小取最大值即可。

 

     AC:

 

/*
TASK:maze1
LANG:C++
ID:sum-g1
*/
#include <cstdio>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;

typedef struct {
    int x,y,step;
}node;
node no[50000];
char Map[205][100];
bool End[205][100],vis[205][100];
int dir[4][2] = {-2,0,0,-2,2,0,0,2};
int n,m,max_step;

bool test(int way,int x,int y) {
    if(way == 0 && Map[x - 1][y] == ' ')   return true;
    if(way == 1 && Map[x][y - 1] == ' ')   return true;
    if(way == 2 && Map[x + 1][y] == ' ')   return true;
    if(way == 3 && Map[x][y + 1] == ' ')   return true;
    return false;
}

int bfs(int x,int y) {
    if(End[x][y]) return 1;

    node a;
    memset(vis,0,sizeof(vis));
    queue<node> q;
    while(!q.empty()) q.pop();

    a.x = x;a.y = y;a.step = 1;
    q.push(a);
    vis[a.x][a.y] = 1;

    while(!q.empty()) {
            node now = q.front();q.pop();
            for(int i = 0;i < 4;i++) {
                    node b;
                    b.x = now.x + dir[i][0];
                    b.y = now.y + dir[i][1];
                    b.step = now.step + 1;
                    if(b.x >= 1 && b.x <= 2 * n - 1 &&
                       b.y >= 1 && b.y <= 2 * m - 1 &&
                       !vis[b.x][b.y] && test(i,now.x,now.y)) {
                                q.push(b);
                                vis[b.x][b.y] = 1;
                                if(End[b.x][b.y]) return b.step;
                       }
            }
    }
}

void init() {
    for(int i = 0;i < 2 * n + 1;i++)
            for(int j = 0;j < 2 * m + 1;j++)
                    End[i][j] = false;

    for(int i = 1;i <= 2 * n - 1;i += 2) {
            if(Map[i][0] == ' ')     End[i][1] = true;
            if(Map[i][2 * m] == ' ') End[i][2 * m - 1] = true;
    }

    for(int j = 1;j <= 2 * m - 1;j += 2) {
            if(Map[0][j] == ' ')     End[1][j] = true;
            if(Map[2 * n][j] == ' ') End[2 * n - 1][j] = true;
    }

    max_step = -1;
}

int main() {
    freopen("maze1.in","r",stdin);
    freopen("maze1.out","w",stdout);

    scanf("%d%d",&m,&n);
    getchar();
    for(int i = 0;i < 2 * n + 1;i++)
            gets(Map[i]);

    init();

    for(int i = 1;i <= 2 * n - 1;i += 2)
            for(int j = 1;j <= 2 * m - 1;j += 2)
                    max_step = max(max_step,bfs(i,j));

    printf("%d\n",max_step);
    return 0;
}

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值