留言 | 关于 | 联系
基础数学 语言相关算法实现 其它知识
返回首页
当前位置: 首页 > 程序设计 > 算法实现 > 迷宫问题讨论--(递归)

迷宫问题讨论--(递归)

时间:2010-03-26 20:50来源:网络 作者:iampolaris 点击:
讲述了迷宫问题从入口到出口的一条路径用递归求解的方法,包括算法描述和实现。

递归的本质就是使用Stack,但是,递归的可读性无疑更好

一、迷宫(maze)的表示

使用二维数组。将整个迷宫看作一张地图,这张地图有m*n个小格子个构成。如果,是墙壁则此出对应的坐标为0,反之为1。----这是一个极好的模型!在许多程序中都可以使用。(比如俄罗斯方块)

二、寻路方式

本质上就是从上下左右四个方向寻找,如果是1则继续,反之退出。

下面这段代码是网上bird网友所作,由于我拿来和堆栈的比较,所以将其列在下面:

 

  1. typedef int STATUS; 
  2.  
  3. void InitMaze(int maze[][10]) 
  4.     maze[1][1]=1; maze[1][2]=1; maze[1][4]=1; 
  5.     maze[1][5]=1; maze[1][6]=1; maze[1][8]=1; 
  6.     maze[2][1]=1; maze[2][2]=1; maze[2][4]=1; 
  7.     maze[2][5]=1; maze[2][6]=1; maze[2][8]=1; 
  8.     maze[3][1]=1; maze[3][2]=1; maze[3][3]=1; 
  9.     maze[3][4]=1; maze[3][7]=1; maze[3][8]=1; 
  10.     maze[4][1]=1; maze[4][5]=1; maze[4][6]=1; 
  11.     maze[4][7]=1; maze[4][8]=1; 
  12.     maze[5][1]=1; maze[5][2]=1; maze[5][3]=1; 
  13.     maze[5][5]=1; maze[5][6]=1; maze[5][7]=1; 
  14.     maze[5][8]=1; 
  15.     maze[6][1]=1; maze[6][3]=1; maze[6][4]=1; 
  16.     maze[6][5]=1; maze[6][7]=1; maze[6][8]=1; 
  17.     maze[7][1]=1; maze[7][5]=1; maze[7][8]=1; 
  18.     maze[8][2]=1; maze[8][3]=1; maze[8][4]=1; 
  19.     maze[8][5]=1; maze[8][6]=1; maze[8][7]=1; 
  20.     maze[8][8]=1; 
  21.  
  22.  
  23.  
  24. void PrintMaze(int maze[][10]) 
  25.     int i,j; 
  26.     printf("\n"); 
  27.     for(i=0;i<10;i++) 
  28.     { 
  29.         for(j=0;j<10;j++) 
  30.         { 
  31.             printf("%d ",maze[i][j]); 
  32.         } 
  33.         printf("\n"); 
  34.     } 
  35.  
  36.  
  37.  
  38. void footPrint(int maze[][10], int posX, int posY) 
  39.     maze[posX][posY]=0; 
  40.  
  41.  
  42.  
  43. void printPos(int posX, int posY) 
  44.     printf("(%d, %d) ", posX, posY); 
  45.  
  46.  
  47.  
  48. STATUS Go(int maze[][10], int entranceX, int entranceY, int exitX, int exitY) 
  49.     if(entranceX==exitX && entranceY==exitY) 
  50.         return 1; 
  51.     if(maze[entranceX][entranceY]==0) 
  52.         return 0; 
  53.      
  54.      
  55.      
  56.     footPrint(maze, entranceX, entranceY); 
  57.     if( Go(maze,entranceX+1,entranceY,exitX,exitY) ){ 
  58.         printPos(entranceX, entranceY); 
  59.         return 1; 
  60.     } 
  61.     if( Go(maze,entranceX,entranceY+1,exitX,exitY) ){ 
  62.         printPos(entranceX, entranceY); 
  63.         return 1; 
  64.     } 
  65.     if( Go(maze,entranceX-1,entranceY,exitX,exitY) ){ 
  66.         printPos(entranceX, entranceY); 
  67.         return 1; 
  68.     } 
  69.     if( Go(maze,entranceX,entranceY-1,exitX,exitY) ){ 
  70.         printPos(entranceX, entranceY); 
  71.         return 1; 
  72.     } 
  73.     return 0; 
  74.  
  75. void main() 
  76.     int maze[10][10]={0}; 
  77.     int entranceX=1, entranceY=1; 
  78.     int exitX=8, exitY=8; 
  79.     InitMaze(maze); 
  80.     PrintMaze(maze); 
  81.     if( !Go(maze,entranceX,entranceY,exitX,exitY) ) 
  82.         printf("Sorry, no solution."); 

 

 

顶一下
(5)
83.3%
踩一下
(1)
16.7%
发表评论
评价:
验证码:点击我更换图片
推荐内容