## 回溯深搜与剪枝初步

2015-04-19 23:31  星星之火✨🔥  阅读(5133)  评论(0编辑  收藏

• 根据问题定义一个解空间，它包含问题的解
• 利用适于搜索的方法组织解空间
• 利用深度优先法搜索解空间，并且在搜索过程中用剪枝函数避免无效搜索

找到一个可能存在的正确的答案

在尝试了所有可能的分步方法后宣告该问题没有答案

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


#include<stdio.h> // 62MS
#include<stdlib.h>
int des_row, des_col;
int m, n, t;
bool escape;
int dir[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}; // dirction:left, up, right, down
int map[7][7];
void dfs(int row, int col, int step);
int main(void)
{
int start_row, start_col;

int wall;
while(scanf("%d%d%d", &n, &m, &t))
{
wall = 0;
escape = false; // 每组测试数据都要重置escape的值,否则肯定WA
if(n == 0 && m == 0 && t == 0)
break;
for(int row = 1; row <= n; row++)
for(int col = 1; col <= m; col++)
{
scanf(" %c", &map[row][col]); // 注意%c前面的空格
if(map[row][col] == 'S')
{
start_row = row;
start_col = col;
}
if(map[row][col] == 'D')
{
des_row = row;
des_col = col;
}
if(map[row][col] == 'X')
wall++;
}
if(n*m - wall - 1 < t) // 定界剪枝,该枝不剪耗时546MS
{
printf("NO\n");
continue;
}
map[start_row][start_col] = 'X';
dfs(start_row, start_col, 0);
if(escape)
printf("YES\n");
else
printf("NO\n");

}

return 0;
}

void dfs(int row, int col, int step) // 因为回溯需要修改step所以它不能定义成全局变量,本函数的三个变量都需要保存在栈中
{
if(row == des_row && col == des_col && step == t)
{
escape = true;
return;
}
if(escape) // 提高效率,缺少的话超时,根据题意只需要确定有无解即可,不必找全解
return;
if(row<1 || row>n || col<1 || col > m) // 出界
return;
int temp = (t-step) - ( abs(des_row-row) + abs(des_col-col) );
if(temp < 0 || (temp&1)) // 定界剪枝+奇偶剪枝(缺少超时),&的优先级大于||(奇偶剪枝可放在main函数中,只需一次判断即可)
return;
for(int i = 0; i < 4; i++)
{
if(map[row+dir[i][0]][col+dir[i][1]] != 'X')
{
map[row+dir[i][0]][col+dir[i][1]] = 'X';
dfs(row+dir[i][0], col+dir[i][1], step+1); // 不要写成step++
map[row+dir[i][0]][col+dir[i][1]] = '.';
}
}
return;
}
// 一点心得&牢骚: // 全不全局是个问题,一方面传参数目要尽量少,另一方面又要避免全局变量过多引起混乱 // 考虑不周的话,DFS很容易就栈溢出或者数组下标越界,因此要考虑好深搜到什么时候终止 // 该代码C++过了,G++ WA,害我反复修改提交了几个小时.不得不喷一下,HDOJ怎么评测的:(

All Rights Reserved.
Author：海峰:)