dfs之前的奇偶判断
题目大意
一个迷宫有N*M格,有一些格子是地板能走,有一些格子是障碍不能走。给一个起点S和一个终点D。一只小狗从S出发,每步走一块地板,在每个地板不能停留,而且走过的地板都不能再走。给定一个T,问小狗能正好走T步到达D吗?
解题思路
详细的题解见罗总博客。
在进行搜索前先进行奇偶判断。
设要求走的步数为 T T T,起点和终点的曼哈顿距离为 t t t,若 T − t T-t T−t 是奇数,一定不存在解;否则可能存在解。
可以举例子来理解(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;
}