现在的位置: 首页 > 综合 > 正文

DFS中的奇偶剪枝(转自chyshnu)

2013年08月26日 ⁄ 综合 ⁄ 共 3539字 ⁄ 字号 评论关闭
文章目录

原文链接http://blog.csdn.net/chyshnu/article/details/6171758

什么是奇偶剪枝?

把矩阵看成如下形式: 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
从为 0 的格子走一步,必然走向为 1 的格子 。
从为 1 的格子走一步,必然走向为 0 的格子 。
即: 
从 0 走向 1 必然是奇数步,从 0 走向 0 必然是偶数步。

所以当遇到从 0 走向 0 但是要求时间是奇数的或者 从 1 走向 0 但是要求时间是偶数的,都可以直接判断不可达!

 

比如有一地图:

 

[c-sharp] view plaincopy

  1. S...  
  2. ....  
  3. ....  
  4. ....  
  5. ...D  

 

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

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

 

[c-sharp] view plaincopy

  1. S..X  
  2. XX.X  
  3. ...X  
  4. .XXX  
  5. ...D  

 

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

就如同上面说的矩阵,要求你从0走到0,无论你怎么绕,永远都是最短距离(偶数步)加上某个偶数步;要求你从1走到0,永远只能是最短距离(奇数步)加上某个偶数步。

 

例题:ZOJ Problem Set - 2110 Tempter of the Bone

题目意思是讲有一只狗要吃骨头,结果进入了一个迷宫陷阱,迷宫里每走过一个地板费时一秒,该地板 就会在下一秒塌陷,所以你不能在该地板上逗留。迷宫里面有一个门,只能在特定的某一秒才能打开,让狗逃出去。现在题目告诉你迷宫的大小和门打开的时间,问你狗可不可以逃出去,可以就输出YES,否则NO。

 

搜索时要用到的剪枝:

1.如果当前时间即步数(step) >= T 而且还没有找到D点,则剪掉。

2.设当前位置(x, y)到D点(dx, dy)的最短距离为s,到达当前位置(x, y)已经花费时间(步数)step,那么,如果题目要求的时间T - step < s,则剪掉。

3. 对于当前位置(x, y),如果,(T-step-s)是奇数,则剪掉(奇偶剪枝)。

4.如果地图中,可走的点的数目(xnum) < 要求的时间T,则剪掉(路径剪枝)。

 

  1. // =====================================================================================  
  2. //   
  3. //       Filename:  zoj2110.cpp  
  4. //    Description:  Tempter of the Bone  
  5. //      Algorithm:  DFS   
  6. //         Status:  RunTime:90ms    RunMemory:188KB   
  7. //        Version:  1.0  
  8. //        Created:  2011年02月01日 14时26分10秒   
  9. //       Revision:  none  
  10. //       Compiler:  g++  
  11. //         Author:  Moose Chan, chyshnu@gmail.com  
  12. //        Company:  SHNU  
  13. //  
  14. // =====================================================================================  
  15. #include <iostream>  
  16. #include <cmath>  
  17. using namespace std;  
  18. int N, M, T, sx, sy, ex, ey;  
  19. char map[6][6];  
  20. const int dir[4][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };  
  21. bool solved =  false, arrd[6][6];  
  22. int Distance ( int x, int y )   
  23. {  
  24.     return abs ( (double)x - ex ) + abs ( (double)y - ey ); // 当前点(x,y)到终点(ex,ey)的最短距离  
  25. }  
  26. void DFS ( int x, int y, int step )  
  27. {  
  28.     if ( solved ) return;  
  29.     if ( map[x][y] == 'D' && step == T ) {  
  30.         solved = true;  
  31.         return;  
  32.     }  
  33.     if ( step >= T ) return;                    // 当前时间即步数(step) >= T 而且还没有找到D点  
  34.     int dis = T - step - Distance ( x, y );  
  35.     if ( dis < 0 || dis % 2 ) return;           // 剩余步数小于最短距离或者满足奇偶剪枝条件  
  36.     for ( int i = 0; i < 4; i += 1 ) {  
  37.         int tx = x + dir[i][0];  
  38.         int ty = y + dir[i][1];  
  39.         int tstep = step + 1;  
  40.         if ( tx >= 0 && tx < N && ty >= 0 && ty < M && map[tx][ty] != 'X' && !arrd[tx][ty]) {  
  41.             arrd[tx][ty] = true;  
  42.             DFS ( tx, ty, tstep );  
  43.             arrd[tx][ty] = false;  
  44.         }  
  45.     }  
  46. }  
  47. int main ( int argc, char *argv[] )  
  48. {  
  49.     while ( cin >> N >> M >> T, N+M+T ) {  
  50.         solved = false;  
  51.         int xnum = 0;                           // 记录'X'的数量  
  52.         for ( int i = 0; i < N; i += 1 ) {  
  53.             cin.get();  
  54.             for ( int j = 0; j < M; j += 1 ) {  
  55.                 cin >> map[i][j];  
  56.                 arrd[i][j] = false;  
  57.                 if ( map[i][j] == 'S' ) {  
  58.                     sx = i;  
  59.                     sy = j;  
  60.                     arrd[i][j] = true;  
  61.                 }  
  62.                 else if ( map[i][j] == 'D' ) {  
  63.                     ex = i;  
  64.                     ey = j;  
  65.                 }  
  66.                 else if ( map[i][j] == 'X' ) {  
  67.                     xnum++;  
  68.                 }  
  69.             }  
  70.         }  
  71.         if ( N * M - xnum > T ) { // 可通行的点必须大于要求的步数,路径剪枝。  
  72.             DFS ( sx, sy, 0 );  
  73.         }  
  74.         if ( solved )  
  75.             cout << "YES" << endl;  
  76.         else   
  77.             cout << "NO" << endl;  
  78.     }  
  79.     return 0;   
  80. }  

 

抱歉!评论已关闭.