简体   繁体   English

如何在C语言中通过递归找到迷宫的路径

[英]How to find a path through a maze with recursion in C

I have to build a recursive function in C language that checks whether there is a path in the matrix ( NxN size that has only 0 and 1 input inside) where I start in the upper left corner and end in the lower right corner. 我必须使用C语言构建一个递归函数,以检查矩阵中是否有路径(NxN大小,内部只有0和1个输入),该路径从左上角开始,在右下角结束。 I'm only allowed to pass through zeros and walk up, down, right and left. 我只允许通过零,然后上下左右走。

I start my path from (0,0) in the top left corner 我从左上角的(0,0)开始路径

I tried this but it is not working well. 我试过了,但是效果不佳。

int x = 0, y = 0;
int isPathExist(char board[][N], int row, int col)
{

    board[x][y] = 1;

    if (x == N - 1 && y == N - 1) {
        return 1;
    }

    if (x + 1 < N && board[x + 1][y] == 0) {
        if (isPathExist(board, x + 1, y)) {
            return 1;
        }
    }

    if (x - 1 >= 0 && board[x - 1][y] == 0) {
        if (isPathExist(board, x - 1, y)) {
            return 1;
        }
    }

    if (y + 1 < N && board[x][y + 1] == 0) {
        if (isPathExist(board, x, y + 1)) {
            return 1;
        }
    }

    if (y - 1 >= 0 && board[x][y - 1] == 0) {
        if (isPathExist(board, x, y - 1)) {
            return 1;
        }
    }

    board[x][y] = 0;

    return 0;
}

The basic (simplest) method is "place your left (or right) hand on a wall". 基本(最简单)的方法是“将左(或右)手放在墙上”。 What this means is a loop that does these steps: 这意味着执行这些步骤的循环:

  • determine which direction to move based on the direction you're facing from the last time you moved, using a clockwise order (eg if you moved north, then check if you can go west, then north, then east, then south). 根据您上次移动时所面对的方向,以顺时针方向确定要移动的方向(例如,如果您向北移动,请检查是否可以先向西,然后向北,然后向东,然后向南)。

  • move in the first direction you determined that you can move 向您确定可以移动的第一个方向移动

  • check if you've been to this location before and discard part of the path you've taken if you have. 请检查您之前是否曾去过此地点,如果有的话,请放弃您已走过的那部分路径。 For example, if you move north into a dead-end and have to move back to the south, modify the path taken so far so that it looks like you never went north in the first place. 例如,如果您向北移动到死胡同,然后又不得不向南移动,请修改目前所走的路径,以使您看起来从未像最初那样向北走。 This is easiest done by numbering your steps - each time you move to a location that you haven't been before, store a "number of times I've moved so far" value at that location so that you can use that value later to make it easier to discard that part of the previously taken path. 这可以通过对步数进行编号来完成,这很容易-每次您移至一个从未有过的位置时,请在该位置存储一个“我已经移动了多少次”值,以便以后可以使用该值可以更轻松地丢弃先前采用的路径的那部分。 The previously taken path can be an array of "location or discarded" values with "number of each move" as the index. 先前采用的路径可以是“位置或已丢弃”值的数组,其中“每个移动的数量”作为索引。

  • check if you've reached the exit, and if you haven't loop back to the start. 检查您是否已经到达出口,以及是否还没有回到起点。

After you've implemented this loop (without recursion) and checked to make sure it works correctly; 在实现此循环后(无递归)并检查以确保其正确运行; you just need to way to make the code suck (slower, harder to read and more likely to crash by running out of stack space) by ramming unnecessary recursion into it somehow. 您只需要通过某种方式将不必要的递归夯入代码,就可以使代码变得更慢(更慢,更难阅读,并且更有可能因耗尽堆栈空间而崩溃)。 The simplest way to do that is to modify the loop so that the last thing ("check if you've reached the exit, and if you haven't loop back to the start") becomes a function call ("check if you've reached the exit, and if you haven't call yourself"). 最简单的方法是修改循环,以使最后一件事(“检查是否到达出口,是否还没有循环回到起点”)成为函数调用(“检查是否已经到达出口,并且如果您还没有打电话给自己,“)。

WARNING: Your questions says "find a path" and this algorithm will do that. 警告: 您的问题是说“找到路径”,而该算法将做到这一点。 However, if there are multiple possible paths, this algorithm may not find the shortest path (or the longest path). 但是,如果有多个可能的路径,则此算法可能找不到最短的路径(或最长的路径)。 For this reason (assuming it's a uni assignment or something) I'd recommend checking the requirements to make sure that "any path" is acceptable. 由于这个原因(假设它是单项作业或其他),我建议检查要求以确保“任何路径”都可接受。

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

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