16

Red

Red Dot - Represents the initial location
Black Dot - Already occupied
Green - Free to occupy
Destination - Boundry of the matrix [which means either x = 0 or y = 0 or x = 8 or y = 8]

eg. Example:

The red dot can place itself only one move at a time and can move in one of green six circles which are attached to it. What will be the fastest method to calculate the shortest path in this type of maze.

jpm
  • 1,042
  • 2
  • 12
  • 36

3 Answers3

4

First of all, you don't need Dijkstra, because all values of the edges are the same. You can use a simple BFS or DFS algorithm. Worst case complexities are the same but I would use BFS as it has better average case complexity. However, O(|V|+|E|) is the fastest you can get here and it's proven.

How to have your graph represented? The best way is to keep a list of neighbours for each Node. Black dots from your example aren't counted as a neighbours. So in your example, each node would have 0(fully covered by black dots) to 6 neighbours. Then, you can get anywhere you can get from any node point via these lists.

BFS algorithm has a property that it assigns each node a layer, which means how far it is from a starting node. You start at your starting point and your current layer will be 0. Then you just follow all nodes from a current layer(usually kept at queue) and try to find it's neighbours(from their list of neighbours), which doesn't have layer assigned and you assign them +1 higher layer. Once you find your Node, (which can still have x,y as attributes for border checking (or property bool border)), at the border of your maze, you know it's as far as your layer value is. If you want to print the exact way, you only have to find way back (via your lists of neighbours) which meets the condition that every step is at -1 layer lower. This will print the way from end to start, but I'm sure you'll get your result with a little help from Stack data structure :)

Marek Židek
  • 809
  • 1
  • 15
  • 31
3

What you have is a "simple" graph problem. The graph connectivity is the legal moves you have. The start node is your red dot. To get a single terminal node, invent one more circle outside the given maze; connect all the real exit nodes to the new circle with a move of zero cost.

Now, apply Dijkstra's algorithm. Done.


Another way to look at the problem is with recursion. Move the red dot, marking the old location black. Recur from the new position. Return when you exit (return path length 1) or have no legal moves (return path length infinite). Keep the shortest known path.

Do those get you un-stuck?

Prune
  • 76,765
  • 14
  • 60
  • 81
-1

A* search algorithm

(from: https://en.wikipedia.org/wiki/A*_search_algorithm )

The following pseudocode describes the algorithm[dubious – discuss]:

function A*(start,goal)
    ClosedSet := {}       // The set of nodes already evaluated.
    OpenSet := {start}    // The set of tentative nodes to be evaluated, initially containing the start node
    Came_From := the empty map    // The map of navigated nodes.


    g_score := map with default value of Infinity
    g_score[start] := 0    // Cost from start along best known path.
    // Estimated total cost from start to goal through y.
    f_score := map with default value of Infinity
    f_score[start] := g_score[start] + heuristic_cost_estimate(start, goal)

    while OpenSet is not empty
       current := the node in OpenSet having the lowest f_score[] value
        if current = goal
            return reconstruct_path(Came_From, goal)

       OpenSet.Remove(current)
       ClosedSet.Add(current)
       for each neighbor of current
           if neighbor in ClosedSet 
               continue     // Ignore the neighbor which is already evaluated.
        tentative_g_score := g_score[current] + dist_between(current,neighbor) // length of this path.
        if neighbor not in OpenSet  // Discover a new node
            OpenSet.Add(neighbor)
        else if tentative_g_score >= g_score[neighbor] 
            continue        // This is not a better path.

        // This path is the best until now. Record it!
        Came_From[neighbor] := current
        g_score[neighbor] := tentative_g_score
        f_score[neighbor] := g_score[neighbor] + heuristic_cost_estimate(neighbor, goal)

return failure

function reconstruct_path(Came_From,current)
    total_path := [current]
    while current in Came_From.Keys:
        current := Came_From[current]
        total_path.append(current)
    return total_path

so, as far as I understand - you can set your start node at the red dot position \ center position, and the goal node as x = 0 or y = 0 or x = 8 or y = 8 (you can make 4 function calls, and take the minimum)

as for the heuristic values for the node - just set the black blocked nodes very high heuristic values, which will make them unreachable, so the algorithm will pass around them.

Aviram Netanel
  • 12,633
  • 9
  • 45
  • 69
  • 2
    It would be nice to give just an idea what the algorithm does. The SO help center says: Why and how are some answers deleted? Answers that do not fundamentally answer the question may be removed. This includes answers that are: … • barely more than a link to an external site – Reinhard Männer Nov 07 '15 at 13:24