简体   繁体   中英

Backtracking maze in C, with obstacles

I need to solve a maze using backtracking method. My maze has 0 listed as a wall, 1 listed as an empty cell, 2 is for visited, 3 for dragon. Dragons are basically obstacles that I can go through BUT I need to choose the path with the LEAST dragons. So far I can solve the maze and mark a path, but I can't seem to think of a relatively simple way of finding the path with the least dragons. Do note we just started coding with C in my uni (so far I've only done java/bash/a bit of python), so I'm really new to C and algorithms in general.

Code is below.

#include <stdio.h>

#define IMPOSSIBLE (N*N+1)

int counter=0;

enum {WALL,EMPTY,VISITED,DRAGON,N};
int printMaze(int maze[N][N])
{
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            printf("%d ",maze[i][j]);
        }
        printf("\n");
    }
}

int solveMaze(int maze[N][N], int i, int j)
{
    if (maze[i][j] == WALL){ return 0; } // If [i][j] are currently a wall (0).
    if (maze[i][j] == VISITED) { return 0; } // If [i][j] are currently a mark (2).
    if (maze[i][j] == DRAGON) { counter++; }
    maze[i][j] = VISITED; // Mark current spot with (2).
    if (i==N-1 && j==N-1) { return 1; } // reached the end (N-1,N-1) - (3,3) incase N is 4.
    if (   ((i < N-1) && solveMaze(maze,i+1,j)) || ((i > 0) && solveMaze(maze,i-1,j)) || ((j < N-1) && solveMaze(maze,i,j+1)) || ((j > 0) && solveMaze(maze,i,j-1)) ) { // checking index-out-bounds + recursively going around the maze
        return 1;
    }
    maze[i][j] = EMPTY;
    return 0;
}

int main() {
    int maze[N][N] = { {1,1,3,3},
                       {3,0,1,1},
                       {3,0,0,1},
                       {1,3,3,1} };

    int solved = solveMaze(maze, 0, 0);
    if (solved)
    {
        printMaze(maze);
        printf("Amount of dragons passed through in the maze: %d\n",counter);
    }
    else
    {
        printf("No solution, %d\n",IMPOSSIBLE);
    }
}

I tried creating a counter that counts the amount of dragons on the way, but I guess I'm not fluent enough in recursions to make it go in every available path and choose the best one.

A way to solve this is to imagine the maze as a 3 dimensional graph instead of 2D. You have the triplet (i, j, number_of_dragons) define the nodes of the graph. Note that this may in extreme case mean the number of nodes are N^4. Thus you will need an array with size [N][N][N*N] to store if the given node is visited (note that dragons and walls can still be stored in a [N][N] array). To get the minimal path from source to target, simply iterate over the the values of [target.x][target.y][i] and return the minimum i for which you have visited the node.

I can suggest a better solution using min path graph algorithm like Dijkstra, but it seems this is out of scope for the question.

You seem to understand the idea of recursively traversing the tree with backtracking. The issue is that you need to find not just a path, but the one with the least cost -- that is, the fewest dragons. That means that in general, you can't stop with the first path you find. You need to keep going until you can be sure that there's no better path.

Here's one way to do it:

  • Maintain a variable to track the number of dragons along the best path discovered so far. Initialize it to something larger than the value that can appear along any path -- your IMPOSSIBLE , for example, or INT_MAX . This is separate from the number of dragons so far encountered along the path you are currently exploring.

  • Perform a recursive traversal much like you are already doing, except

    • Fix the example code's problems with restoring state (node contents and counter value) when you backtrack out of a dragon node.
    • Stop exploring any given path when the number of dragons encountered along it equals the number along the best path discovered so far.
    • In light of the previous, you can be certain that whenever you discover a path that reaches the exit, that path has fewer dragons than the best path previously discovered, if any. Update the variable tracking the best number of dragons accordingly. Note that this does not necessarily end the search, but it may reduce the search space that remains to be explored.

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