Rat in a Maze

来源:互联网 发布:杭州少儿趣味编程 编辑:程序博客网 时间:2024/05/21 07:14
 

Geeks 面试题: Rat in a Maze 回溯法解迷宫 

标签: Geeks面试题Rat in a Maze回溯法解迷宫
 1632人阅读 评论(3) 收藏 举报
 分类:
 

We have discussed Backtracking and Knight’s tour problem in Set 1. Let us discuss Rat in a Maze as another example problem that can be solved using Backtracking.

A Maze is given as N*N binary matrix of blocks where source block is the upper left most block i.e., maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A rat starts from source and has to reach destination. The rat can move only in two directions: forward and down.
In the maze matrix, 0 means the block is dead end and 1 means the block can be used in the path from source to destination. Note that this is a simple version of the typical Maze problem. For example, a more complex version can be that the rat can move in 4 directions and a more complex version can be with limited number of moves.

Following is an example maze.

 Gray blocks are dead ends (value = 0). 

Following is binary matrix representation of the above maze.

                {1, 0, 0, 0}                {1, 1, 0, 1}                {0, 1, 0, 0}                {1, 1, 1, 1}

Following is maze with highlighted solution path.

Following is the solution matrix (output of program) for the above input matrx.

                {1, 0, 0, 0}                {1, 1, 0, 0}                {0, 1, 0, 0}                {0, 1, 1, 1} All enteries in solution path are marked as 1.

Naive Algorithm
The Naive Algorithm is to generate all paths from source to destination and one by one check if the generated path satisfies the constraints.

while there are untried paths{   generate the next path   if this path has all blocks as 1   {      print this path;   }}

Backtrackng Algorithm

If destination is reached    print the solution matrixElse   a) Mark current cell in solution matrix as 1.    b) Move forward in horizontal direction and recursively check if this        move leads to a solution.    c) If the move chosen in the above step doesn't lead to a solution       then move down and check if  this move leads to a solution.    d) If none of the above solutions work then unmark this cell as 0        (BACKTRACK) and return false.


Backtracking 回溯法的三部曲:

1 初始化原始数据,开始点

2 判断下一步是否合法,如果合法就继续递归搜索答案,如果不合法就返回

3 递归直到找到答案,返回真值

这里只需要找到一个解就可以,所以只要找到一个解就可以马上返回。

Leetcode上很喜欢找出所有解的,那么就需要递归所以可能路径了。


说明一下,原题意是规定往下和往右这两个方向探索的,相当于简化了。

这里给出的程序是可以上下左右四个方向探索的,相当于全功能版的解迷宫了,当然,回溯算法是OK的了,类可以继续扩展。

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. class Maze  
  2. {  
  3.     vector<vector<bool> > maze;  
  4.     vector<vector<bool> > solMaze;  
  5.     int row, col;  
  6. public:  
  7.     Maze():maze(6), row(6), col(6), solMaze(6, vector<bool>(6))  
  8.     {  
  9.         bool a[6][6] = {  
  10.             {0,1,0,0,0,0},  
  11.             {0,0,0,1,1,0},  
  12.             {1,1,1,0,0,0},  
  13.             {0,0,0,0,1,1},  
  14.             {1,1,0,1,0,0},  
  15.             {0,0,0,0,0,0}};  
  16.         for (int i = 0; i < 6; i++)  
  17.         {  
  18.             maze[i].assign(a[i], a[i]+6);  
  19.         }  
  20.     }  
  21.     bool isLegal(int x, int y)  
  22.     {  
  23.         return x>=0 && y>=0 && x<col && y<row && !maze[x][y] && !solMaze[x][y];  
  24.     }  
  25.   
  26.     bool solveMaze(int x, int y)  
  27.     {  
  28.         if (x == col-1 && y == row-1)   
  29.         {  
  30.             solMaze[x][y] = 1;  
  31.             return true;  
  32.         }  
  33.   
  34.         if (isLegal(x, y))  
  35.         {  
  36.             solMaze[x][y] = 1;  
  37.   
  38.             if (solveMaze(x, y+1)) return true;  
  39.             if (solveMaze(x+1, y)) return true;  
  40.             if (solveMaze(x-1, y)) return true;  
  41.             if (solveMaze(x, y-1)) return true;  
  42.   
  43.             solMaze[x][y] = 0;  
  44.         }  
  45.         return false;  
  46.     }  
  47.   
  48.     void printSolution()  
  49.     {  
  50.         for (auto x:solMaze)  
  51.         {  
  52.             for (auto y:x)  
  53.                 cout<<y<<" ";  
  54.             cout<<endl;  
  55.         }  
  56.     }  
  57. };  
  58.   
  59. int main()  
  60. {  
  61.     cout<<"C++ Class Solution\n";  
  62.     Maze ma;  
  63.     ma.solveMaze(0,0);  
  64.     ma.printSolution();  
  65.   
  66.     system("pause");  
  67.     return 0;  
  68. }  



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝吃了硅胶乳贴怎么办 用完卫生巾后阴部有些不舒服怎么办 指甲上有荧光剂怎么办 小孩吃了荧光剂怎么办 毛巾上有荧光剂怎么办 用过劣质面膜后怎么办 液体硅胶奶嘴煮完有味怎么办 后跟贴粘在鞋上怎么办 优化营商环境公安怎么办 提升营商环境公安怎么办 准予迁入证明过期了怎么办 粉底液容易脱妆怎么办 家人进了火疗传销怎么办 自发热护膝洗了怎么办 用气垫bb卡粉怎么办 贴药膏后皮肤过敏红肿怎么办 贴完膏药皮肤痒怎么办 猕猴桃吃的嘴疼怎么办 摩拜单车怎么办月卡 出国忘了带护照怎么办 雅漾喷雾失压了怎么办 洗衣服时衣服粘上卫生纸怎么办 一晚上卫生巾都是满的怎么办 宝宝头上痱子痒怎么办 短裤里的宽松紧带拧了怎么办 肉色内衣被染黑色了怎么办 安全裤总往上缩怎么办 夏天穿裙子膝盖怕凉怎么办 夏天穿裙子膝盖冷怎么办 天凉嗓子痒咳嗽怎么办 棉服里面跑毛怎么办 棉衣里面的棉一块一块的怎么办 穿姨妈巾悟出痱子怎么办 穿裙子上衣太长了怎么办 微信封号了零钱怎么办 快递加盟商欠我工资怎么办 加盟费交了以后怎么办 加盟总部违约加盟商该怎么办 自行车锁钥匙丢了怎么办 假体隆胸肿胀痛怎么办 恶露60天不干净怎么办