Overfencing(BFS)

本文介绍了一个迷宫逃脱问题,通过宽度优先搜索(BFS)算法找到从任意起点到两个出口中最远距离的最小步数。该算法能够确保从迷宫内的任一位置都能以最短路径到达最近的出口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

 

 

 

### 配置 Windows 10 中的 VSCode 右键菜单 为了实现通过右键菜单在 Windows 10 上使用 Visual Studio Code 打开文件或文件夹,可以通过修改注册表来完成此操作。以下是具体方法: #### 修改注册表以支持文件夹右键打开 需要向系统的注册表中添加特定条目以便于在右键菜单中显示“使用VSCode打开”的选项。 创建一个新的注册表项 `HKEY_CLASSES_ROOT\Directory\shell\VSCode` 并设置其默认值为 `"open folder as VSCode"` 表示该选项的功能描述[^1]。 接着指定图标的路径,这一步是可选的,但推荐执行以增强用户体验。为此可以新增一个名为 `Icon` 的字符串值并将其指向 VSCode 安装目录下的 `Code.exe` 路径。 对于实际运行命令的部分,则需进一步建立子项 `[HKEY_CLASSES_ROOT\Directory\Background\VSCode\Command]` 和对应的默认值,这个值应设为完整的启动参数形式,即包含程序路径以及 `%V` 参数用于传递当前所选目标的位置信息[^3]。 #### 添加背景上下文中的快捷方式 如果希望不仅限于选定的具体项目而是整个资源管理器内的任意位置都能调用 VSCode ,则还需要处理另一个分支——`HKEY_CLASSES_ROOT\Directory\Background\shell` 下的内容。在此基础上重复上述过程,只是这次的名字可以根据个人喜好定制比如叫作 “使用VSCode打开”,同样记得关联正确的图标与动作指令。 另外还有一种更简便的方式就是利用软件自带功能或者第三方工具达成相似效果而无需手动调整系统级设定;不过按照原生途径操作能够确保最大程度上的兼容性和稳定性[^2]。 最后提醒一点,在做任何涉及更改操作系统核心配置之前最好先做好备份工作以防万一出现问题时能迅速恢复原始状态。 ```python # 示例 Python 脚本展示如何自动化部分流程 (仅作为参考用途) import os from winreg import * key_path = r"Directory\shell\VSCode" command_key_path = key_path + r"\Command" try: # 创建主键 with CreateKey(HKEY_CLASSES_ROOT, key_path) as key: SetValueEx(key, None, 0, REG_SZ, "open folder as VSCode") # 设置图标 icon_value = "\"C:\\Program Files\\Microsoft VS Code\\Code.exe\"" with OpenKey(HKEY_CLASSES_ROOT, key_path, 0, KEY_SET_VALUE) as key: SetValueEx(key, 'Icon', 0, REG_SZ, icon_value) # 建立命令子键及其值 command_value = "\"C:\\Program Files\\Microsoft VS Code\\Code.exe\" \"%V\"" with CreateKey(HKEY_CLASSES_ROOT, command_key_path) as cmd_key: SetValueEx(cmd_key, None, 0, REG_SZ, command_value) except Exception as e: print(f"Error occurred: {e}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值