FOJ--1046--Tempter of the Bone(dfs+奇偶剪枝)

本文介绍了一个关于迷宫中狗狗逃逸的经典问题及其算法解决方案。该问题涉及在一个N*M的矩形迷宫中找到从起点到门的特定时间路径。迷宫包含墙壁、空地、起点和门等元素。通过使用深度优先搜索算法,并结合奇偶剪枝策略来优化搜索过程,最终确定狗狗能否在门打开的瞬间到达门口。
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze. 

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him. 
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following: 

'X': a block of wall, which the doggie cannot enter; 
'S': the start point of the doggie; 
'D': the Door; or 
'.': an empty block. 

The input is terminated with three 0's. This test case is not to be processed. 
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise. 
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO

YES

思路:深搜寻找时间为特定时间的路径,这个题涉及了奇偶剪枝,我故意把它放在了深搜的外面,其实奇偶剪枝就是说:如果两个路径同起点,共终点,那么他们的时间差一定是偶数。其实很简单,以一条路径为基准,如果另一条偏离了他,假如他向上偏了一格,那么他迟早还会向下一格,因为他们最后重合,这是已知的,一上一下就当然是偶数了。其它方向类似。所以在dfs之前就可以剪枝了。

#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <iostream>
using namespace std;

char mp[110][110];
int mark[110][110];
int m,n,xz,yz,T;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
bool yes;
void dfs(int x,int y,int t)
{
    if(yes) return;
    if(t > T) return;
    if(mp[x][y] == 'D')
    {
        if(t == T)
            yes = 1;
        return;
    }
    for(int i = 0; i < 4; i++)
    {
        int x_ = x+dir[i][0];
        int y_ = y+dir[i][1];
        if(x_ < 1 || x_ > m || y_ < 1 || y_ > n) continue;
        if(mp[x_][y_] == 'X') continue;
        if(mark[x_][y_] == 1) continue;
        mark[x_][y_] = 1;
        dfs(x_,y_,t+1);
        mark[x_][y_] = 0;
    }
}

int main()
{
    while(cin >> m >> n >> T&&m+n+T)
    {
        int x,y;
        memset(mark,0,sizeof(mark));
        for(int i = 1; i <= m; i++)
        {
            for(int j = 1; j <= n; j++)
            {
               cin >> mp[i][j];
               if(mp[i][j] == 'S')
                   x = i, y = j;
               if(mp[i][j] == 'D')
                   xz = i,yz = j;
            }
        }
        mark[x][y] = 1,yes = 0;
        int tmp = xz-x+yz-y-T;//剪枝
        if(tmp%2)
        {
            printf("NO\n");
            continue;
        }
        dfs(x,y,0);
        printf(yes?"YES\n":"NO\n");
    }
    return 0;
}




### 解决 pip 安装包时遇到的连接超时和文件被占用问题 当使用 `pip` 命令安装 Python 包时,可能会因为网络环境或其他原因导致 **连接超时 (connection timed out)** 或者由于目标文件正在被其他进程访问而引发 **WinError 32 文件正由另一个程序使用** 的错误。以下是针对这些问题的具体解决方案。 #### 连接超时问题 如果在运行 `pip install` 时遇到了连接超时的情况,可以尝试以下方法: 1. 使用国内镜像源加速下载过程。例如阿里云、清华大学 TUNA 镜像站等提供了稳定的 PyPI 镜像服务[^1]。 ```bash pip install package_name -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 2. 设置全局配置文件来永久更改默认索引地址。对于 Windows 用户,在 `%APPDATA%\pip\pip.ini` 中添加如下内容[^2]: ``` [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple timeout = 60 retries = 5 ``` 3. 如果仍然存在不稳定情况,则可以通过增加重试次数或者延长等待时间参数手动调整行为: ```bash pip install package_name --timeout=60 --retries=10 ``` #### 文件被占用问题 (WinError 32) Windows 平台上经常会出现因权限不足或资源冲突引起的 “file in use, WinError 32” 错误消息。处理这类问题的方法有以下几个方面: 1. 关闭任何可能锁定该路径下文件的应用程序实例(比如 IDE 编辑器),再重新执行命令[^3]. 2. 添加 `--user` 参数指定只对当前用户的环境中进行操作而不是试图修改系统范围内的库目录结构。这通常能绕过管理员权限需求并减少潜在干扰因素的影响[^4]. ```bash pip install django==5.2.1 --user ``` 3. 升级到最新版本的 Pip 工具本身也可能有助于修复某些已知缺陷以及提高兼容性和稳定性表现[^5]. 可通过下面指令完成自我更新流程: ```bash python -m pip install --upgrade pip setuptools wheel ``` 4. 对于特别顽固的情形考虑卸载后再干净地重装依赖项以清除残留数据影响正常工作流的可能性[^6]: ```bash pip uninstall django && pip install django==5.2.1 --user ``` ```python import sys print(sys.executable) # 确认使用的Python解释器位置是否正确无误 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值