-2

I have this maze:

    ##++##############
    ##++++++++++++++##
    ########++########
    ##++++++++##----##
    ##++########--####

The "#" symbols are walls in the maze.

The "+" regions of the maze are reachable regions of the maze from the entry point, and the "-" region of the maze are unreachable from the entry point. The entry point is located at the top of the maze.

What I would like to do now is plot a cost of the reachable regions in the maze, which will look like this:

    ##00##############
    ##++02++04++06++##
    ########++########
    ##++08++06##----##
    ##10########--####

This shows that the maze has a cost of 10 from entry to exit.

The maze up top is stored in a 2d array and I used recursion to solve the reachable zones. How can I label costs of the paths using my recursive function, which looks like this:

    void
    flood_fill(m_t * maze, int row, int col) {
        // If row,col is outside maze
        if ( row < 0 || row >= maze->height || col < 0 || col >= maze->width) return;
        // If row,col is not open
        if (maze->M[row][col].type != '.') return;

        // Mark row,col as part of path.
        maze->M[row][col].type = '+';

        // Go LEFT
        flood_fill(maze, row, col - 1);
        // Go DOWN
        flood_fill(maze, row + 1, col);
        // Go RIGHT
        flood_fill(maze, row, col + 1);
        // Go UP
        flood_fill(maze, row - 1, col);

        return;
    }

For the first maze, I used this recursive function at the top of the maze, and it flood filled all the reachable cells with "+".

Are there any suggestions as to how I can do something similar to this but with the path costs instead. I'm just looking for some examples or suggestions as to how I might go about it. Any help on how I can go about achieving the second maze example would be helpful.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75

1 Answers1

1

Pass an extra parameter that is the path cost so far.

flood_fill(m_t * maze, int row, int col, int cost)

Each maze location gets an additional attribute, .cost, which you update as you flood-fill the maze. Initialize the cost to MAXINT as a marker. For instance,

    if (maze->M[row][col].cost > cost)
      maze->M[row][col].cost = cost

    // Go LEFT
    flood_fill(maze, row, col - 1, cost+1);
    // repeat for the other 3 directions

In any case, you now have the cost to each square. Display as desired when you dump the maze to the screen.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • 1
    note : Cost of a point might not be the shortest since it is a depth-first search. – BLUEPIXY May 18 '16 at 23:14
  • 1
    Right. If there is a circuit in the maze, the depth-first algorithm may fill in a high value before a lower one. You'd need to check for an existing value before assigning. – Prune May 18 '16 at 23:16
  • 1
    Right. You have all the data at your "fingertips" when you print the maze. With the cost filled in, you can check at each maze location and decide whether you want to print **++** or a numeric value. I can't give specific changes, since you didn't include maze-printing code. I expect you can do that part on your own ... ? – Prune May 19 '16 at 00:27
  • 1
    There is a need to change `if (maze->M[row][col].type != '.') return; maze->M[row][col].type = '+';`. – BLUEPIXY May 19 '16 at 01:06
  • Actually, you probably want to use MAXINT, a cost larger than anything you'll generate in a maze. That way, any actual cost will be cheaper, and you fill it in automatically. Then there's no need to check for a marker value. – Prune May 19 '16 at 16:11
  • I don't see a problem with multiple entries -- literally, because you haven't provided the logic you use to flood_fill from different starting points. "It doesn't work" is not a problem statement. Also, consider that this may qualify as a separate question. – Prune May 19 '16 at 16:16
  • For every maze location (all rows & cols) set **maze->M[row][col].cost = MAXINT**. If grabbing MAXINT from **limits.h** is too much trouble, just pick a large number that's easy to recognize, such as 987654321. All you need is to have it larger than any path length you'll generate. – Prune May 19 '16 at 22:57
  • Start at the end and work backwards, going to the neighboring square with a cost one less than the one you're on. If there's more than one such neighbor, either choice is a shortest path. You might want to look up basic graph traversal algorithms; that's what you have with a maze. – Prune May 20 '16 at 16:04