DFS算法迷宫生成器

时间:2017-04-13 15:57:11

标签: c algorithm depth-first-search maze

我试图使用DFS算法在ASCII中创建一个迷宫('#'代表一堵墙,而#39;一个自由空间)作为开始左上角和右下角出口。问题是迷宫开始创建然后被阻止,因为它的所有邻居都已被访问过。

我从左上角开始,将单元格标记为已访问并放置一个' ' (它代表一个自由空间),然后我随机选择了一个小区的邻居,我也是这样做的。不过我把它放在一个循环中,我确定这不是个好主意。

这是我对DFS的尝试:

int     generation(t_maze *maze, int pos_y, int pos_x)                                                                                                                                                              
{                                                                                                                                                                                                                   
  int   dest;                                                                                                                                                                                                       

  maze->maze[pos_y][pos_x] = ' ';                                                                                                                                                                                   
  maze->visited[pos_y][pos_x] = '1';                                                                                                                                                                                
  while (maze->maze[maze->height - 1][maze->width - 1] == '#')                                                                                                                                                      
    {                                                                                                                                                                                                               
      if ((dest = my_rand(1, 4)) == 1 && pos_y - 1 >= 0 && maze->visited[pos_y - 1][pos_x] == '0')                                                                                                                  
        generation(maze, pos_y - 1, pos_x);                                                                                                                                                                         
      else if (dest == 2 && pos_x + 1 < maze->width && maze->visited[pos_y][pos_x + 1] == '0')                                                                                                                      
        generation(maze, pos_y, pos_x + 1);                                                                                                                                                                         
      else if (dest == 3 && pos_y + 1 < maze->height && maze->visited[pos_y + 1][pos_x] == '0')                                                                                                                     
        generation(maze, pos_y + 1, pos_x);                                                                                                                                                                         
      else if (dest == 4 && pos_x - 1 >= 0 && maze->visited[pos_y][pos_x - 1] == '0')                                                                                                                               
        generation(maze, pos_y, pos_x - 1);                                                                                                                                                                         
      my_showtab(maze->maze); //it prints the 2d array                                                                                                                                                                                      
      usleep(50000);                                                                                                                                                                                                
    }  


typedef struct  s_maze                                                                                                                                                                                              
{                                                                                                                                                                                                                   
  int           width;                                                                                                                                                                                              
  int           height;                                                                                                                                                                                             
  char          **maze;                                                                                                                                                                                             
  char          **visited;                                                                                                                                                                                          
}               t_maze; 

在结构中, 宽度是迷宫的宽度 高度是迷宫的高度 迷宫是一个二维阵列,应该填充&#39; &#39;和&#39;#&#39; visit是一个2D数组,0和1,0:未访问,1:访问

我想要一个这样的迷宫(小例子)

 ########
      # #
 ##     #
 #  #
#######  

1 个答案:

答案 0 :(得分:0)

您的代码构建了一条路径,因为它始终只传递给下一个单元格。这不是dfs。你可以这样做:

def dfs(x, y):
    visited[x][y] = true
    maze[x][y] = ' '
    next_cell = random unvisited neighbors of (x, y):
    dfs(next_cell.x, next_cell.y)

重点是:你需要在某个时候回溯(为它使用递归是很方便的)。一条单独的路径不会看起来像你想要的那样(它也可能卡住而且永远不会到达出口)。