回溯法+奇偶剪枝——Hdu 1010 Tempter of the Bone

来源:互联网 发布:linux密码忘了 编辑:程序博客网 时间:2024/05/19 13:24

1)   题目

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 47967    Accepted Submission(s): 12905


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

 

2)    题意

在一个n行m列的迷宫中,每一步只能向上、下、左、右中任意方向走一格,迷宫中有围墙的地方是无法到达的。从起点s开始,能否刚好走t步,到达e。


3)    数据范围

迷宫大小最大为6*6,测试数据组数最大为50组。感觉上数据量很小,可实际上,如果在6*6的迷宫中,枚举所有可能路径的话,时间复杂度是指数级的,所以剪枝是关键。


4)    算法

先用BFS判断s到e是否有路径,以及这条最短路径长度是否小于等于t,然后再进行回溯法+奇偶剪枝。

迷宫中回溯法的剪枝——奇偶剪枝


5)    代码

[cpp] view plaincopyprint?
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cstring>  
  4. #include <queue>  
  5.   
  6. using namespace std;  
  7.   
  8. #define MAXSIZE 10  
  9.   
  10. int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};  
  11.   
  12. char maze[MAXSIZE][MAXSIZE];  
  13. bool vis[MAXSIZE][MAXSIZE];  
  14. int dx, dy;  
  15. bool finished;  
  16.   
  17. void InitMaze(int m, int n)  
  18. {  
  19.     int i;  
  20.     for (i = 0; i < m+2; i++)  
  21.     {  
  22.         maze[i][0] = maze[i][n+1] = 'X';  
  23.     }  
  24.     for (i = 0; i < n+2; i++)  
  25.     {  
  26.         maze[0][i] = maze[m+1][i] = 'X';  
  27.     }  
  28. }  
  29.   
  30. void Backtrack(int x, int y, int rt)  
  31. {  
  32.     vis[x][y] = true;  
  33.     if (x == dx && y == dy)  
  34.     {  
  35.         if (rt == 0)  
  36.         {  
  37.             finished = true;  
  38.         }  
  39.     }  
  40.     else  
  41.     {  
  42.         int i;  
  43.         for (i = 0; i < 4; i++)  
  44.         {  
  45.             int nextx = x + dir[i][0];  
  46.             int nexty = y + dir[i][1];  
  47.             if (!vis[nextx][nexty] && maze[nextx][nexty] != 'X')  
  48.             {  
  49.                 Backtrack(nextx, nexty, rt-1);  
  50.                 if (finished)  
  51.                 {  
  52.                     return;  
  53.                 }  
  54.             }  
  55.         }  
  56.     }  
  57.     vis[x][y] = false;  
  58. }  
  59.   
  60. struct Position  
  61. {  
  62.     int x, y;  
  63.     int nsteps;  
  64. };  
  65.   
  66. int BFS(int x, int y)  
  67. {  
  68.     queue<Position> next;  
  69.     Position currPos = {x, y, 0};  
  70.     next.push(currPos);  
  71.     vis[x][y] = true;  
  72.   
  73.     while (!next.empty())  
  74.     {  
  75.         currPos = next.front();  
  76.         next.pop();  
  77.   
  78.         if (currPos.x == dx && currPos.y == dy)  
  79.         {  
  80.             return currPos.nsteps;  
  81.         }  
  82.   
  83.         int i;  
  84.         for (i = 0; i < 4; i++)  
  85.         {  
  86.             Position nextPos = currPos;  
  87.             nextPos.x += dir[i][0];  
  88.             nextPos.y += dir[i][1];  
  89.             if (!vis[nextPos.x][nextPos.y] && maze[nextPos.x][nextPos.y] != 'X')  
  90.             {  
  91.                 nextPos.nsteps++;  
  92.                 next.push(nextPos);  
  93.                 vis[nextPos.x][nextPos.y] = true;  
  94.             }  
  95.         }  
  96.     }  
  97.     return -1;  
  98. }  
  99.   
  100. int Dist(int x, int y)  
  101. {  
  102.     return abs(dx-x)+abs(dy-y);  
  103. }  
  104.   
  105. int main(void)  
  106. {  
  107.     int m, n, t;  
  108.     while (scanf("%d%d%d", &m, &n, &t) != EOF)  
  109.     {  
  110.         getchar();  
  111.         if (m == 0 && n == 0 && t == 0)  
  112.         {  
  113.             break;  
  114.         }  
  115.   
  116.         InitMaze(m, n);  
  117.         int sx, sy;  
  118.         int i, j;  
  119.         for (i = 1; i <= m; i++)  
  120.         {  
  121.             for (j = 1; j <= n; j++)  
  122.             {  
  123.                 maze[i][j] = getchar();  
  124.                 if (maze[i][j] == 'S')  
  125.                 {  
  126.                     sx = i;  
  127.                     sy = j;  
  128.                 }  
  129.                 if (maze[i][j] == 'D')  
  130.                 {  
  131.                     dx = i;  
  132.                     dy = j;  
  133.                 }  
  134.             }  
  135.             getchar();  
  136.         }  
  137.           
  138.         finished = false;  
  139.         if (Dist(sx, sy) % 2 == t % 2) //奇偶剪枝  
  140.         {  
  141.             memset(vis, falsesizeof(vis));  
  142.             int minNSteps = BFS(sx, sy);  
  143.             if (minNSteps != -1 && minNSteps <= t)  
  144.             {  
  145.                 memset(vis, falsesizeof(vis));  
  146.                 Backtrack(sx, sy, t);  
  147.             }  
  148.         }  
  149.         if (finished)  
  150.         {  
  151.             puts("YES");  
  152.         }  
  153.         else  
  154.         {  
  155.             puts("NO");  
  156.         }  
  157.     }  
  158.     return 0;  
  159. }  


6)    测试数据

4 4 5

S.X.

..X.

..XD

....

3 4 5

S.X.

..X.

...D

5 5 9

...D.

X.XX.

.XX..

SX...

....X

7)    提交结果

0 0