当前位置:   article > 正文

dfs + 回溯 +剪枝_dfs+回溯+减枝

dfs+回溯+减枝

hdu  1010

Tempter of the Bone

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


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
 

参考某位大牛得剪枝思路。。

题目大意:这道题就是讲有一只狗要吃骨头,结果进入了一个迷宫陷阱,迷宫里每走过一个地板费时一秒,该地板就会在下一秒塌陷,所以你不能在该地板上逗留。迷宫里面有一个门,只能在特定的某一秒才能打开,让狗逃出去。现在题目告诉你迷宫的大小和门打开的时间,问你狗可不可以逃出去,可以就输出YES,否则NO。
解题思路:这道题,要用到剪枝搜索来做,否则会超时。剪掉的条件是,如果可走地板数目小于给定的时间,绝对不可能得救。还有就是狗走到门的时间必须和题目给定的时间是同奇同偶的,否则也不能在指定的那秒到达门,也不可能得救,剪掉这两种情况后。就用深度搜索来做。从起点出发,深搜周围的路,走过的路就标记为不可走,一直搜索下去,如果搜索失败就回溯,恢复原数据,把可能的路都搜索一遍过去,看看是否有可行方案。
关于剪枝,没有剪枝的搜索不太可能,一个是奇偶剪枝,一个是路径剪枝
奇偶剪枝:
把矩阵标记成如下形式:
0,1,0,1,0
1,0,1,0,1
0,1,0,1,0
1,0,1,0,1
很明显,如果起点在0 而终点在1 那显然 要经过奇数步才能从起点走到终点,依次类推,奇偶相同的偶数步,奇偶不同的奇数步
在读入数据的时候就可以判断,并且做剪枝,当然做的时候并不要求把整个矩阵0,1刷一遍,读入的时候起点记为(Si,Sj) 终点记为(Di,Dj) 判断(Si+Sj) 和 (Di+Dj) 的奇偶性就可以了
路径剪枝:
矩阵的大小是N*M 墙的数量记为num 如果能走的路的数量 N*M - num 小于时间T,就是说走完也不能到总的时间的,这显然是错误的,可以直接跳出了
剪枝3:就是记录当前点到终点的最短路,如果小于剩余的时间的话,就跳出

什么也不加很裸很裸地 TLE 代码:

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. int n,m,T;
  5. char map[10][10],vis[10][10];
  6. bool survive;
  7. int dir[][2]= {{0,1},{0,-1},{1,0},{-1,0}};
  8. void dfs(int x,int y,int tim)
  9. {
  10. //cout<<x<<" "<<y<<" "<<tim<<endl;
  11. if(tim>T||survive) return;
  12. if(map[x][y]=='D'&&tim==T)
  13. {
  14. survive=1;
  15. return;
  16. }
  17. vis[x][y]=1;
  18. for(int i=0; i<4&&!survive; i++)
  19. {
  20. int nx=x+dir[i][0];
  21. int ny=y+dir[i][1];
  22. if(nx<0||nx>=n) continue;
  23. if(ny<0||ny>=m) continue;
  24. if(vis[nx][ny]||map[nx][ny]=='X') continue;
  25. //cout<<nx<<" "<<ny<<endl;
  26. dfs(nx,ny,tim+1);
  27. }
  28. vis[x][y]=0;
  29. }
  30. int main()
  31. {
  32. int sx,sy;
  33. while(cin>>n>>m>>T&&(n||m||T))
  34. {
  35. for(int i=0; i<n; i++)
  36. for(int j=0; j<m; j++)
  37. {
  38. cin>>map[i][j];
  39. if(map[i][j]=='S') sx=i,sy=j;
  40. }
  41. memset(vis,0,sizeof(vis));
  42. survive=0;
  43. // cout<<sx<<" "<<sy<<endl;
  44. dfs(sx,sy,0);
  45. if(survive) cout<<"YES"<<endl;
  46. else cout<<"NO"<<endl;
  47. }
  48. return 0;
  49. }


Accepted101062MS472K1115 BG++

加上剪枝后的代码:

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. int n,m,T;
  5. char map[10][10],vis[10][10];
  6. bool survive;
  7. int dir[][2]= {{0,1},{0,-1},{1,0},{-1,0}};
  8. int sx,sy,ex,ey;
  9. int abs(int x)
  10. {
  11. return x<0?-x:x;
  12. }
  13. void dfs(int x,int y,int tim)
  14. {
  15. //if(f[x][y][tim]!=-1) return;
  16. if(tim>T||survive) return;
  17. if(map[x][y]=='D'&&tim==T)
  18. {
  19. survive=1;
  20. return;
  21. }
  22. if(abs(x-ex)+abs(y-ey)>T-tim) return;
  23. vis[x][y]=1;
  24. for(int i=0; i<4&&!survive; i++)
  25. {
  26. int nx=x+dir[i][0];
  27. int ny=y+dir[i][1];
  28. if(nx<0||nx>=n) continue;
  29. if(ny<0||ny>=m) continue;
  30. if(vis[nx][ny]||map[nx][ny]=='X') continue;
  31. //cout<<nx<<" "<<ny<<endl;
  32. dfs(nx,ny,tim+1);
  33. }
  34. vis[x][y]=0;
  35. }
  36. int main()
  37. {
  38. while(cin>>n>>m>>T&&(n||m||T))
  39. {
  40. int num=0;
  41. for(int i=0; i<n; i++)
  42. for(int j=0; j<m; j++)
  43. {
  44. cin>>map[i][j];
  45. if(map[i][j]=='S') sx=i,sy=j;
  46. if(map[i][j]=='D') ex=i,ey=j;
  47. if(map[i][j]!='X') num++;
  48. }
  49. if(num-1<T) {cout<<"NO"<<endl; continue;}
  50. memset(vis,0,sizeof(vis));
  51. // memset(f,-1,sizeof(f));
  52. survive=0;
  53. if((sx+sy+ex+ey+T)%2==0)
  54. dfs(sx,sy,0);
  55. if(survive) cout<<"YES"<<endl;
  56. else cout<<"NO"<<endl;
  57. }
  58. return 0;
  59. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/907321
推荐阅读
相关标签
  

闽ICP备14008679号