【dfs+搜索之前的奇偶判断】HDU-1010 Tempter of the Bone

本文介绍了如何解决一个关于小狗在迷宫中寻找目标的路径问题。在进行深度优先搜索(DFS)之前,通过奇偶判断优化搜索条件。如果要求的步数(T)与起点到终点的曼哈顿距离(t)具有相同的奇偶性,则可能存在解。否则,直接判断为无解。通过实例解释了奇偶性不变的规律,并提供了相应的C++代码实现。

dfs之前的奇偶判断

Tempter of the Bone

题目大意

一个迷宫有N*M格,有一些格子是地板能走,有一些格子是障碍不能走。给一个起点S和一个终点D。一只小狗从S出发,每步走一块地板,在每个地板不能停留,而且走过的地板都不能再走。给定一个T,问小狗能正好走T步到达D吗?

解题思路

详细的题解见罗总博客

在进行搜索前先进行奇偶判断。

设要求走的步数为 T T T,起点和终点的曼哈顿距离为 t t t,若 T − t T-t Tt 是奇数,一定不存在解;否则可能存在解。

可以举例子来理解(S 代表起点,D 代表终点):

如果开始的位置如下:

. . D
. . .
S . .

两点的曼哈顿距离为 2 + 2 = 4 2 + 2=4 2+2=4

如果按照这样的路径走:

. . D
    |
. . .
    |
S-.-.

可以看出要走 4 4 4 步。

如果不这样走,在原来的路径上拐个弯:

. . D
    |
.-. .
| |  |
S .-.

可以看出,现在要走 6 6 6 步,也就是在原来的基础上加了 2 2 2 步,不改变原来步数的奇偶性。在题目中就可以理解为,由于障碍物的作用,可能要在最短路径的基础上进行绕弯,但是最终步数的奇偶性是不会改变的。

多试几个例子,可以得到这样的规律: T T T t t t 要同奇偶。

利用这一判断条件,在进行搜索之前就可以进行优化。

参考代码

#include<stdio.h>
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<algorithm>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<stack>
//#include<unordered_map>
//#include<unordered_set>
//#define LOCAL  //提交时一定注释
#define VI vector<int>
#define eps 1e-6
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long LL;
typedef double db;
const int inf = 0x3f3f3f3f;
const LL INF = 1e18;
const int N = 17;
#define ls rt << 1
#define rs rt << 1 | 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
inline int readint() {int x; scanf("%d", &x); return x;}

int dir[][2] = {
        {0, -1}, {0, 1}, {1, 0}, {-1, 0}
};

int n, m, t;
char M[10][10];
int vis[10][10];

struct node{
    int x, y;
};
bool ok(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < m && M[x][y] != 'X';
}

int man(node x, node y) {
    return abs(x.x - y.x) + abs(x.y - y.y);
}
bool flag;
node s, e;
void dfs(int x, int y, int num) {
    if (flag || num > t) return;
    if (x == e.x && y == e.y) {
        if (num == t) {
            flag = true;
        }
        return;
    }
    for(int i = 0; i < 4; ++i) {
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        if (!ok(nx, ny) || vis[nx][ny]) continue;
        vis[nx][ny] = 1;
        dfs(nx, ny, num + 1);
        vis[nx][ny] = 0;
    }
}

void init() {
    flag = false;
    memset(vis, 0, sizeof(vis));
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
//   freopen("output.txt", "w", stdout);
#endif
    while (~scanf("%d%d%d", &n, &m, &t) && n) {
        init();
        for(int i = 0; i < n; ++i) {
            scanf("%s", M[i]);
            for(int j = 0; j < m; ++j) {
                if (M[i][j] == 'S') {
                    s.x = i, s.y = j;
                }
                if (M[i][j] == 'D') {
                    e.x = i, e.y = j;
                }
            }
        }
        if ((t - man(s, e)) & 1) {
            printf("NO\n");
            continue;
        }
        vis[s.x][s.y] = 1;
        dfs(s.x, s.y, 0);
        printf("%s\n", flag ? "YES" : "NO");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值