Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

c - Finding a cost for a maze path

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...