简体   繁体   English

使用DFS解决迷宫

[英]Maze Solving using DFS

In a school project, we got to solve a maze given through program parameter. 在一个学校项目中,我们必须解决通过程序参数给出的迷宫。 To achieve this, we need to use Depth First Search algorithm. 为此,我们需要使用深度优先搜索算法。

I have been able to find the pseudo code of a DFS algorithm, and even recode it using C. The algorithm is able to find the exit, however now I'm looking for a way to get the path from the beginning to the end of the maze. 我已经能够找到DFS算法的伪代码,甚至可以使用C重新编码它。该算法能够找到出口,但是现在我正在寻找一种方法来获取从DFS的开始到结束的路径。迷宫。

The beginning of each maze is the upper left corner, and the end is the bottom right corner. 每个迷宫的起点是左上角,终点是右下角。

Initial Maze (X = Walls ; * = Free Space): 初始迷宫(X =墙壁; * =自由空间):

*****XX****X********XXXX
XX******XX***XXXXX***XXX
XX***XXXX**XXXXX****XXXX
XX***XXXXXXXXXXXXXX****X
*****XXXXXX****XX***XXXX
XX*************XXXX*****

Solved Maze (o = Path from begining to end): 解决了迷宫(o =从开始到结束的路径):

oooooXXooooXooooooooXXXX
XX**ooooXXoooXXXXX*o*XXX
XX***XXXX**XXXXX***oXXXX
XX***XXXXXXXXXXXXXXo***X
*****XXXXXX****XX**oXXXX
XX*************XXXXooooo

Here is the code I have been able to produce so far: 这是到目前为止我已经能够生成的代码:

#include "../include/depth.h"

static t_bool   stack_push(t_list **stack, int x, int y)
{
  t_cell    *cell;

  if (!(cell = malloc(sizeof(t_cell))))
    return (FALSE);
  cell->coord.x = x;
  cell->coord.y = y;
  if (!my_list_push(stack, cell))
    {
      free(cell);
      return (FALSE);
    }
  return (TRUE);
}

static t_bool   is_colored(const t_list *colored, int x, int y)
{
  while (colored != NULL)
    {
      if (((t_cell *) colored->elm)->coord.x == x &&
      ((t_cell *) colored->elm)->coord.y == y)
    return (TRUE);
      colored = colored->next;
    }
  return (FALSE);
}

static t_bool   push_edges(t_map *map, t_stack *stack, int x, int y)
{
  if (x - 1 >= 0 && !is_colored(stack->colored, x - 1, y))
    stack_push(&stack->stack, x - 1, y);
  if (x + 1 < map->sz.x && !is_colored(stack->colored, x + 1, y))
    stack_push(&stack->stack, x + 1, y);
  if (y - 1 >= 0 && !is_colored(stack->colored, x, y - 1))
    stack_push(&stack->stack, x, y - 1);
  if (y + 1 < map->sz.y && !is_colored(stack->colored, x, y +1))
    stack_push(&stack->stack, x, y + 1);
  return (TRUE);
}

static t_bool   exit_properly(t_stack *stack, void *curr)
{
  my_list_destroy(&stack->stack, LIST_FREE_PTR, NULL);
  my_list_destroy(&stack->colored, LIST_FREE_PTR, NULL);
  free(curr);
  return (TRUE);
}

t_bool      depth(t_map *map)
{
  t_stack   stack;
  t_cell    *curr;

  stack.colored = stack.stack = NULL;
  stack_push(&stack.stack, MAP_START_X, MAP_START_Y);
  while (stack.stack != NULL)
    {
      curr = stack.stack->elm;
      my_list_pop(&stack.stack, &stack.stack);
      if (curr->coord.x == map->sz.x - 1 &&
      curr->coord.y == map->sz.y - 1)
    return (exit_properly(&stack, curr));
      if (!is_colored(stack.colored, curr->coord.x, curr->coord.y))
    {
      stack_push(&stack.colored, curr->coord.x, curr->coord.y);
      push_edges(map, &stack, curr->coord.x, curr->coord.y);
    }
      free(curr);
    }
  return (TRUE);
}

The initial algorithm: 初始算法:

1  procedure DFS-iterative(G,v):
2      let S be a stack
3      S.push(v)
4      while S is not empty
5          v = S.pop()
6          if v is not labeled as discovered:
7              label v as discovered
8              for all edges from v to w in G.adjacentEdges(v) do 
9                  S.push(w)

Thanks. 谢谢。

Note: t_list types holds a generic linked list. 注意:t_list类型包含一个通用链表。

Assuming that your input data is in the form of a matrix: 假设您的输入数据采用矩阵形式:

Create a structure with the following definition 使用以下定义创建结构

struct parent {
    int x, y;
};

Create a two-dimensional matrix with the above struct as its data type and of the same dimesions as of the input matrix. 创建一个二维矩阵,将上述结构作为其数据类型,并将其维数与输入矩阵的维数相同。

struct parent ** parent_info = (struct parent **)malloc(ROW_SIZE * sizeof(struct parent* ));
int i = 0;
for (i = 0; i < ROW_SIZE; i++) {
    parent_info[i] = (struct parent *)malloc(COLUMN_SIZE * sizeof(struct parent));
}

where ROW)_SIZE and COLUMN_SIZE are the number of rows and columns respectively in your input matrix. 其中ROW)_SIZE和COLUMN_SIZE分别是输入矩阵中的行数和列数。

Now each time you push a new graph node (matrix cell) in your stack (pseudo code line 9), set the parent details in the parent_info matrix (eg in pseudo code, set 'v' as the parent of 'w'). 现在,每次将新的图节点(矩阵单元)推入堆栈(伪代码第9行)时,请在parent_info矩阵中设置父详细信息(例如,在伪代码中,将“ v”设置为“ w”的父)。

For example, you move from (0,0) to (0,1) then set 例如,您从(0,0)移至(0,1),然后设置

parent_info[0][1].x = 0;
parent_info[0][1].y = 0;

Finally, to retrieve the path, recursively follow the co-ordinates in the parent_info matrix, starting from the parent co-ordinates of the end point of the maze. 最后,要检索路径,请从迷宫端点的父坐标开始,递归地遵循parent_info矩阵中的坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM