简体   繁体   English

在C ++递归错误中找到通过迷宫的路径

[英]Find a path through a maze in C++ recursion error

I'm trying to write a function which finds a path through a maze. 我正在尝试编写一个找到通过迷宫的路径的函数。 There is another function reads the maze from a text file. 还有另一个功能是从文本文件中读取迷宫。 For now the function I wrote to find the path works fine when the maze is 20 rows and 20 columns but it does not work with any other variations. 现在,当迷宫为20行20列时,我编写的用于查找路径的函数可以正常工作,但不适用于任何其他变体。 Here is is the code I've wrote for the function: 这是我为该函数编写的代码:

void find_paths(maze m, int r, int c, int rows, int cols) {
  if (r == rows - 1)
    display(m, rows, cols);
  else {
    if (r > 0 && m[r - 1][c] == space) // UP
    {
      m[r - 1][c] = path;
      find_paths(m, r - 1, c, rows, cols);
      m[r - 1][c] = space;
    }
    if (m[r + 1][c] == space) // DOWN
    {
      m[r + 1][c] = path;
      find_paths(m, r + 1, c, rows, cols);
      m[r + 1][c] = space;
    }
    if (m[r][c - 1] == space) // LEFT
    {
      m[r][c - 1] = path;
      find_paths(m, r, c - 1, rows, cols);
      m[r][c - 1] = space;
    }
    if (m[r][c + 1] == space) // RIGHT
    {
      m[r][c + 1] = path;
      find_paths(m, r, c + 1, rows, cols);
      m[r][c + 1] = space;
    }
  }
}

space is a char =' ' path is a char ='.' 空格是一个字符=''路径是一个字符='。

And here is a screenshot for the file of the maze that the program reads. 这是程序读取的迷宫文件的屏幕截图。

maze.txt

And here is a screenshot for the program execution. 这是程序执行的屏幕截图。

执行

I'm new to C++ hope you can help me. 我是C ++的新手,希望您能为我提供帮助。

As mentioned in a comment by Benjamin Barrois you need a way to know that you checked out a route and if so not take it again. 正如本杰明·巴鲁瓦(Benjamin Barrois)的评论中所提到的那样,您需要一种方法来知道您已经签出了一条路线,如果不这样做,就不要再走了。

I would suggest another character than the '.' 我建议使用除“。”以外的其他字符。 (path) to distinguish dead-ends from the actual solution although even that is not exactly enough. (路径)以将死胡同与实际解决方案区分开,尽管这还不够。

So I would add a variable named visited = 'x'; 因此,我将添加一个名为visited = 'x';的变量visited = 'x'; and use that on return. 并将其用于回报。

Also, you check r > 0 in the first test (UP) but you did not check any such limit in the other if() (DOWN, LEFT, RIGHT). 同样,您在第一个测试(UP)中检查r > 0 ,但没有在另一个if() (DOWN,LEFT,RIGHT)中检查任何这样的限制。 Although if you have the correct characters there, it will work (ie it will hit a wall first) but if that's not the case, you'll start hitting the wrong characters. 尽管如果那里有正确的字符,它将起作用(即,它将首先碰到墙),但是如果不是这样,您将开始遇到错误的字符。

void find_paths(maze m, int r, int c, int rows, int cols) {
  if (r == rows - 1)
    display(m, rows, cols);
  else {
    if (r > 0 && m[r - 1][c] == space) // UP
    {
      m[r - 1][c] = path;
      find_paths(m, r - 1, c, rows, cols);
      m[r - 1][c] = visited;
    }
    if (r + 1 < rows && m[r + 1][c] == space) // DOWN
    {
      m[r + 1][c] = path;
      find_paths(m, r + 1, c, rows, cols);
      m[r + 1][c] = visited;
    }
    if (c > 0 && m[r][c - 1] == space) // LEFT
    {
      m[r][c - 1] = path;
      find_paths(m, r, c - 1, rows, cols);
      m[r][c - 1] = visited;
    }
    if (c + 1 < cols && m[r][c + 1] == space) // RIGHT
    {
      m[r][c + 1] = path;
      find_paths(m, r, c + 1, rows, cols);
      m[r][c + 1] = visited;
    }
  }
}

That being said, your current algorithm doesn't stop when it finds a solution since it is recursive, it will go on forever. 话虽如此,您的当前算法在找到解决方案时不会停止,因为它是递归的,它将永远持续下去。 If you want to be able to bail out, your function needs to return true (success!) or false (continue search). 如果您希望能够纾困,则您的函数需要返回true(成功!)或false(继续搜索)。

Although the idea to use a recursive mechanism can be useful to find all the solutions and especially (if you were to calculate that) the length of the solution so you return only the shorter solution (which is often what a maze search is about.) 尽管使用递归机制的想法对于找到所有解特别有用(特别是(如果要计算的话))解的长度,所以您只返回较短的解(这通常是迷宫搜索的意义所在)。

bool find_paths(maze m, int r, int c, int rows, int cols) {
  if (r == rows - 1) {
    display(m, rows, cols);
    return true;
  }
  else {
    if (r > 0 && m[r - 1][c] == space) // UP
    {
      m[r - 1][c] = path;
      if(find_paths(m, r - 1, c, rows, cols)) {
        return true;
      }
      m[r - 1][c] = visited;
    }
    if (r + 1 < rows && m[r + 1][c] == space) // DOWN
    {
      m[r + 1][c] = path;
      if(find_paths(m, r + 1, c, rows, cols) {
        return true;
      }
      m[r + 1][c] = visited;
    }
    if (c > 0 && m[r][c - 1] == space) // LEFT
    {
      m[r][c - 1] = path;
      if(find_paths(m, r, c - 1, rows, cols) {
        return true;
      }
      m[r][c - 1] = visited;
    }
    if (c + 1 < cols && m[r][c + 1] == space) // RIGHT
    {
      m[r][c + 1] = path;
      if(find_paths(m, r, c + 1, rows, cols) {
        return true;
      }
      m[r][c + 1] = visited;
    }
  }
  return false;
}

As a side note, this is not specific to C++. 附带说明,这并非特定于C ++。

There's not much wrong (although it could be done more efficiently). 没什么大不了的(尽管可以做得更有效)。 Most likely it's not working because you are missing bounds checks for columns. 最有可能是它不起作用,因为您缺少列的边界检查。 Since you allow the columns to obtain any value, you're probably going outside the bounds of the maze. 由于允许列获取任何值,因此您可能超出了迷宫的范围。 Your other problem is that when you reach the solution, you still have all the other recursive paths being taken. 您的另一个问题是,当您找到解决方案时,仍然需要采用所有其他递归路径。

This is just a minor change from your code and should work: 这只是您的代码的一个小改动,应该可以起作用:

bool done = false;

void find_paths(maze m, int r, int c, int rows, int cols) {
  if(done) {
      return;
  }
  if (r == rows - 1) {
    display(m, rows, cols);
    done = true;
  } else {
    if (r > 0 && m[r - 1][c] == space) // UP
    {
      m[r - 1][c] = path;
      find_paths(m, r - 1, c, rows, cols);
      m[r - 1][c] = space;
    }
    if (m[r + 1][c] == space) // DOWN
    {
      m[r + 1][c] = path;
      find_paths(m, r + 1, c, rows, cols);
      m[r + 1][c] = space;
    }
    if (c > 0 && m[r][c - 1] == space) // LEFT
    {
      m[r][c - 1] = path;
      find_paths(m, r, c - 1, rows, cols);
      m[r][c - 1] = space;
    }
    if (c < cols && m[r][c + 1] == space) // RIGHT
    {
      m[r][c + 1] = path;
      find_paths(m, r, c + 1, rows, cols);
      m[r][c + 1] = space;
    }
  }
}

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

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