-5

I've been working on trying to solve mazes and output the path from start to the end. No matter what I do though, it never really works 100%. It manages to backtrack trough the whole maze like it should and find the end... but actually outputting ONLY the path that it should take won't work. It works on some of the generated mazes, but not generally. It also includes some of the dead-ends.

The problem is that i've probably tried 10 different DFS algorithms online, but none of them works. It's very confusing that so many different sites seem to have algorithms that doesn't work, i've implemented them practically literally.

Would be very kind if someone could give an actual working pseudocode of how to write a simple maze solver using stack that shows the relevant path to take. After 20-25 hours of experimenting I don't think I will get anywhere anymore.

Alex S
  • 3
  • 2
    Please choose one of your attempts and post it here. Then we can debug it together :-) – Kevin May 05 '15 at 13:50
  • You question is a little confusing as it seems to say that is works "like it should" but also "never really works 100%". Perhaps a small example of what you would expect, and what you actually get would help here. – gilleain May 05 '15 at 13:53
  • Can't post code, have changed it about 80 times so it won't make any sense. The depth-first-searching works, but I want an algorithm that show the direct path from START-END. Not the whole DFS. – Alex S May 05 '15 at 14:14
  • If you've got the DFS tree, and you know the goal node, you can reconstruct the path to the start node. – beaker May 05 '15 at 16:17

1 Answers1

0

not exactly pseudocode, but it should work

define node: {int id}
define edge: {node a , b ; int weight (default=1)}

define listNeighbours:
input: node n
output: list of neighbours

define dfs:
input: list nodes, list edges, node start, node end
output: list path

bool visited[length(nodes)]
fill(visited , false)

list stack
add(stack , start)
visited[start.id] = true

while ! isEmpty(stack)
     node c = first(stack)

     if c.id == end.id
          return stack

     list neighbours = listNeighbours(c)
     bool allVisited = true
     node next

     for node n in neighbours
          if visited[n.id]
                continue
          else
                allVisited = false
                next = n

      if allVisited
          pop(stack)
      else
          push(stack , next)

return null

NOTE: ids of nodes are always <= the total number of nodes in the graph and unique in the graph