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 876353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:25:07+00:00 2026-05-15T11:25:07+00:00

What are the possible ways to solve a maze? Ive got two ideas, but

  • 0

What are the possible ways to solve a maze?
Ive got two ideas, but I think they are not very elegant.

Base situation: We have a matrix, and the elements in this matrix are ordered in a way that it represents a maze, with one way in, and one out.

My first idea was to send a robot through the maze, following one side, until it’s out of the maze. I think this is a very slow solution.

The second one passes through every successive item marked with 1, checks where it can go (up, right, down, left) chooses one way and it continues its path there. This is even slower than the first one.

Of course it’s a bit faster if I make the two bots multi-threaded at every junction, but thats also not the best way.

There needs to be better solutions to send a bot through a maze.

EDIT
First: Thanks for the nice answers!

The second part of my question is: What to do in the case if we have a multi-dimensional graph? Are there special practices for that, or is the answer of Justin L. usable for that too?
I think it’s not the best way for this case.

The third question:
Which of these maze solver algorithms is/are the fastest? (Purely hypothetically)

  • 1 1 Answer
  • 1 View
  • 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-05-15T11:25:07+00:00Added an answer on May 15, 2026 at 11:25 am

    You can think of your maze as a tree.

         A
        / \
       /   \
      B     C
     / \   / \
    D   E F   G
       / \     \
      H   I     J
     / \
    L   M
       / \
      **  O
    
    (which could possibly represent)
    
            START
            +   +---+---+
            | A   C   G |
        +---+   +   +   +
        | D   B | F | J |
    +---+---+   +---+---+
    | L   H   E   I |
    +---+   +---+---+
        | M   O |
        +   +---+
        FINISH
    
    (ignoring left-right ordering on the tree)
    

    Where each node is a junction of paths. D, I, J, L and O are dead ends, and ** is the goal.
    Of course, in your actual tree, each node has a possibility of having as many as three children.

    Your goal is now simply finding what nodes to traverse to to find the finish. Any ol’ tree search algorithm will do.

    Looking at the tree, it’s pretty easy to see your correct solution by simply “tracing up” from the ** at the deepest part of the tree:

    A B E H M **
    

    Note that this approach becomes only slightly more complicated when you have “loops” in your maze (i.e., when it is possible, without backtracing, you re-enter a passage you’ve already traversed through). Check the comments for one nice solution.

    Now, let’s look at your first solution you mentioned, applied to this tree.

    Your first solution is basically a Depth-First Search, which really isn’t that bad. It’s actually a pretty good recursive search. Basically, it says, “Always take the rightmost approach first. If nothing is there, backtrack until the first place you can go straight or left, and then repeat.

    A depth-first search will search the above tree in this order:

    A B D (backtrack) E H L (backtrack) M ** (backtrack) O (backtrack thrice) I
    (backtrack thrice) C F (backtrack) G J
    

    Note that you can stop as soon as you find the **.

    However, when you actually code a depth-first search, using recursive programming makes makes everything much easier. Even iterative methods work too, and you never have to explicitly program how to backtrack. Check out the linked article for implementations.

    Another way of searching a tree is the Breadth-First solution, which searches through trees by depth. It’d search through the above tree in this order:

    A (next level) B C (next level) D E F G (next level)
    H I J (next level) L M (next level) ** O
    

    Note that, due to the nature of a maze, breadth-first has a much higher average amount of nodes it checks. Breadth-first is easily implementing by having a queue of paths to search, and each iteration popping a path out of a queue, “exploding it” by getting all of the paths that it can turn into after one step, and putting those new paths at the end of the queue. There are no explicit “next level” commands to code, and those were just there to aid in understanding.

    In fact, there is a whole expansive list of ways to search a tree. I’ve just mentioned the two simplest, most straightforward way.

    If your maze is very, very long and deep, and has loops and crazies, and is complicated, I suggest the A* algorithm, which is the industry standard pathfinding algorithm which combines a Breadth-First search with heuristics…sort of like an “intelligent breadth-first search”.

    It basically works like this:

    1. Put one path in a queue (the path where you only walk one step straight into the maze). A path has a “weight” given by its current length + its straight-line distance from the end (which can be calculated mathematically)
    2. Pop the path with the lowest weight from the queue.
    3. “Explode” the path into every path that it could be after one step. (i.e., if your path is Right Left Left Right, then your exploded paths are R L L R R and R L L R L, not including illegal ones that go through walls)
    4. If one of these paths has the goal, then Victory! Otherwise:
    5. Calculate the weights of the exploded paths, and put all of them back into the queue (not including the original path)
    6. Sort the queue by weight, lowest first. Then repeat from Step #2

    And that’s A*, which I present specially highlighted because it is more or less the industry standard pathfinding algorithm for all applications of pathfinding, including moving from one edge of the map to another while avoiding off-road paths or mountains, etc. It works so well because it uses a shortest possible distance heuristic, which gives it its “intelligence”. A* is so versatile because, given any problem, if you have a shortest possible distance heuristic available (ours is easy — the straight line), you can apply it.

    BUT it is of great value to note that A* is not your only option.

    In fact, the wikipedia category of tree traversal algorithms lists 97 alone! (the best will still be on this page linked earlier)

    Sorry for the length =P (I tend to ramble)

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

Sidebar

Related Questions

Oftentimes a developer will be faced with a choice between two possible ways to
What are all the possible ways in which we can get memory leaks in
There are several possible ways of getting the path to the application data directory:
This is one of the possible ways I come out: struct RetrieveKey { template
Is it possible to pass objects (serializable classes or other ways) to a Silverlight
What are some good ways to do this? Is it even possible to do
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
I wonder if there are any good / possible ways to automate - for
I know its probably possible, but is it practical and doable to try and
I know its probably possible, but is it practical and doable to try and

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.