Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:07:17+00:00 2026-06-04T06:07:17+00:00

I have a recursive maze solver algorithm that can successfully work its way through

  • 0

I have a recursive maze solver algorithm that can successfully work its way through the maze. The only problem is that I can not find a way to save the shortest path between the starting point and the ending point. How would I save the coordinates of the shortest path?

This is the recursive function

void Solve_Maze(int coorx,int coory) {
    if((coorx>=0)&&(coorx<l)&&(coory>=0)&&(coory<w)) {
        if((Map[coorx][coory]==Start)||(Map[coorx][coory]==path)) {
            Map[coorx][coory]=visited;
            Solve_Maze(coorx+1,coory);
            Solve_Maze(coorx-1,coory);
            Solve_Maze(coorx,coory+1);
            Solve_Maze(coorx,coory-1);
        }else if(Map[coorx][coory]==End) {
            delete Map;
            Solved=true;
        }
    }
}

After adding a vector to store the coordinates I got this output

(1,2)
(2,2)
(3,2)
(4,2)
(5,2)
(6,2)
(7,2)
(8,2)
(9,2)
(7,3)
(7,4)
(7,5)
(7,6)
(8,6)
(8,7)
(9,7)
(10,7)

It stores all of the coordinates but it even stores the coordinates of the path we do not want to take ((7,2)(8,2)(9,2) and then back to (7,3)). Is there a way that I can store just store the shortest path?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:07:19+00:00Added an answer on June 4, 2026 at 6:07 am

    Here’s how you can keep track of the solution coordinates using a vector:

    #include <iostream>
    #include <vector>
    #include <map>
    
    using namespace std;
    
    const int w = 10, l = 10;
    const char Start = 'S', Path = ' ', End = 'E', Visited = '.', Solution = '*';
    
    char Map[w][l + 1] =
    {
        { "  #  # #  " },
        { " S   #  # " },
        { " ###      " },
        { "   #   #  " },
        { "    #   # " },
        { "#        #" },
        { "    ###   " },
        { "  ##E ##  " },
        { "  #       " },
        { "   ##### #" },
    };
    
    int Solved = false;
    vector<pair<int,int> > SolutionCoors;
    
    void PrintMap()
    {
        int x, y;
    
        for (y = 0; y < w; y++)
        {
            for (x = 0; x < l; x++)
                cout << Map[y][x];
    
            cout << endl;
        }
    }
    
    void Solve_Maze(int CoorX, int CoorY)
    {
        if (CoorX >= 0 && CoorX < l && CoorY >= 0 && CoorY < w && !Solved)
        {
            SolutionCoors.push_back(make_pair(CoorX, CoorY)); // Store potential solution
    
            if (Map[CoorY][CoorX] == Start || Map[CoorY][CoorX] == Path)
            {
                Map[CoorY][CoorX] = Visited;
    
                Solve_Maze(CoorX + 1, CoorY);
                Solve_Maze(CoorX - 1, CoorY);
                Solve_Maze(CoorX, CoorY + 1);
                Solve_Maze(CoorX, CoorY - 1);
            }
            else if (Map[CoorY][CoorX] == End)
                Solved = true;
    
            if (!Solved)
                SolutionCoors.pop_back();
        }
    }
    
    int main()
    {
        PrintMap();
    
        Solve_Maze(1, 1);
    
        if (Solved)
        {
            for (vector<pair<int,int> >::iterator it = SolutionCoors.begin();
                 it != SolutionCoors.end();
                 it++)
            {
                cout << "(" << it->first << "," << it->second << ")" << endl; // Print solution coords
                Map[it->second][it->first] = Solution; // Also mark on the map
            }
    
            PrintMap();
        }
    
        return 0;
    }
    

    Output:

      #  # #  
     S   #  # 
     ###      
       #   #  
        #   # 
    #        #
        ###   
      ##E ##  
      #       
       ##### #
    (1,1)
    (2,1)
    (3,1)
    (4,1)
    (4,2)
    (5,2)
    (6,2)
    (6,3)
    (5,3)
    (5,4)
    (6,4)
    (7,4)
    (7,5)
    (8,5)
    (8,6)
    (9,6)
    (9,7)
    (8,7)
    (8,8)
    (7,8)
    (6,8)
    (5,8)
    (4,8)
    (4,7)
      #  #.#..
     ****#..#.
     ###***...
       #.**#..
        #***#.
    #      **#
        ### **
      ##* ##**
      #.*****.
       ##### #
    

    Also note that my Map[][] has reversed coordinates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a recursive method that returns categories, and checks for its sub categories.
I have a recursive algorithm which steps through a string, character by character, and
How can my program exit a function call? I have a recursive function that,
I have a recursive template so that I can update a count. In the
I have a recursive method on the base form that takes in a control
Lets say I have a recursive function that creates lists within lists. It return
I have a recursive immutable data structure in ocaml which can be simplified to
i have not worked with XML in a while can someone post the syntax
So, I have a page that loads and through jquery.get makes several requests to
I have a recursive scalar function that needs to update a record in another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.