DFS中的奇偶剪枝

什么是奇偶剪枝?

比如有一地图:

S...  

....  

....  

....  

...D  

要求从S点到达D点,此时,从S到D的最短距离为s = abs ( dx - sx ) + abs ( dy - sy )。

如果地图中出现了不能经过的障碍物:

S..X  

XX.X  

...X  

.XXX  

...D  

此时的最短距离s' = s + 4,为了绕开障碍,不管偏移几个点,偏移的距离都是最短距离s加上一个偶数距离。

其偏移的距离一定是偶数距离,因为一部分远离最短路径,一部分回归最短路径,他们是对称的,必定是一个偶数。如果给你起点和终点的最短距离X,询问Y步能否到达终点,那么Y-X如果不是偶数则必定无法到达!

HDU1010 Tempter of the Bone

Problem Description

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

小狗为了在T时恰好到达终点,它不一定走的是最短路径,可能为了到达T而多走步数,则用到了奇偶剪枝。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
char map[10][10];
int n, m, t, flag;
int sx, sy, ex, ey;
int vis[10][10], dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int dis(int x, int y)                                //计算该点离终点的最短距离
{
	return (abs(x - ex) + abs(y - ey));
}
void dfs(int x, int y, int steep)
{
	if (flag)                                   //剪枝,如果满足了题目要求可以达到则不必再继续递归了
		return;
	if (steep == t&&x == ex&&y == ey)     
	{
		flag = 1;                          //满足要求标记一下
		return;
	}
	if (steep>=t||t - steep - dis(x, y) < 0 )
		return;         //剪枝,如果当前步数超出t或当前剩余步数小于最短距离,不可能达到了
    if((t - steep - dis(x, y)) % 2 == 1)
        return;                 //剪枝,如果是奇数的话不可能到达,奇偶剪枝
	for (int i = 0; i < 4; i++)
	{
		if (x + dx[i] >= 1 && x + dx[i] <= n&&y + dy[i] >= 1 && y + dy[i] <= m&&map[x + dx[i]][y + dy[i]] != 'X'&&vis[x + dx[i]][y + dy[i]] == 0)
		{
			vis[x + dx[i]][y + dy[i]] = 1;          //标记+递归+回溯
			dfs(x + dx[i], y + dy[i], steep + 1);
			vis[x + dx[i]][y + dy[i]] = 0;
		}
	}
}
int main()
{
	while (scanf("%d%d%d", &n, &m, &t) != EOF)
	{
		if (n == 0 && m == 0 && t == 0)
			break;
		int num = 0;
		flag = 0;
		memset(vis, 0, sizeof(vis));
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				cin >> map[i][j];
				if (map[i][j] == 'S')
				{
					sx = i; sy = j;
					vis[i][j] = 1;
				}
				if (map[i][j] == 'D')
				{
					ex = i; ey = j;
				}
				if (map[i][j] == 'X')
					num++;
			}
		}
		if (n*m - num > t)               //剪枝,如果可行的地方数小于t的话,一定到达不了
			dfs(sx, sy, 0);
		if (flag)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/slience_646898/article/details/81232968
今日推荐