简体   繁体   中英

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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