I'm working on a brand new grid based game, and I need some help. I'm searching for algorithm (or code) that can iterate through all possible paths between point A and point B on a 6x6 grid (taking into account to not walk through empty squares). So this isn't a shortest path pathfinding problem, but an all paths pathfinding problem.

'' ------------------------------------------------------------------'' A* Demonstration using Manhattan Distance Hueristic'' by coderJeff - November 2007'' ------------------------------------------------------------------'''' The intent of this demonstration was to provide a sample program that '' displays results similar to "A* Pathfinding for Beginners" tutorial '' by Patrick Lester, found at:'' http://www.policyalmanac.org/games/aStarTutorial.htm'''' Compiled using FreeBASIC 0.18.3 - however, early versions of FreeBASIC'' should work.'''''' INSTRUCTIONS'' ------------'' LEFT mouse button to set the starting tile'' RIGHT mouse button to set the ending tile'' MIDDLE mouse button to toggle the solid tiles'' ESCAPE key to exit'''' ------------------------------------------------------------------

"All possible paths" however, could be quite a lot of paths... basically it would be, in its simplest form, all squares that connect with the target square eventually. Perhaps re-consider whether you really need "all possible paths". Maybe you actually just need to know all the squares that connect to your target? For that, you just need to do a "painting" or "filling" algorithm.

It seems that I didn't define the problem very well. You can't use the same cell if you're searching for current path (for example red path), but you can use the same cell if you're searching for alternative path.

What you described will give you millions of solutions for even a moderately filled 20x20 grid.
Maybe try calling breadth-first search repeatedly, and fill traversed points for each path you find until you run out of solutions.

duke4e wrote:Anyone at least having idea how to implement brute force?

some things we know about the problem:

let dns = the north-south distance between a and b
let dwe = the east-west distance between a and b
let min = dns + dwe
then the shortest possible path between a & b will be at least min moves long.

let i be the number of squares in the grid that are marked impassable at the begining of the problem.
let max = 35 - i
then the longest path between a & b cannot be more than max moves long

let the character set n,s,w,e, stand for moves to the north, south east and west from the current position.

then a starting set of possible solutions for brute force test, many of which would be illegal, would be the set of all possible strings using the characters nswe that are at least min characters long but not more than max characters long.

write a routine to generate all of them, (basically a min to max digit long base 4 counting routine) discard the invalid ones

obviously invalid:
- any with a substring of the same letter repeated more than 5 times consecutively
- any where (count n) - (count s) <> dns
- any where (count w) - (count e) <> dwe

step through the rest one move at a time, add to "bad list" discard if move goes off grid, onto an impassible square, onto a previously visited square.

add to good list if move goes onto square b (found one)

then we can also say that any new possible sequence that has a member of the bad list as a prefix is also bad

and we can also say that any new possible sequence that has a member of the good list as a prefix is also bad because it is longer than it legally can be

That's all that occurs to me for now.

Last edited by j_milton on Oct 17, 2010 4:50, edited 2 times in total.