Newsgroups: comp.ai.games
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!udel!gatech!howland.reston.ans.net!ee.und.ac.za!psgrain!fizban.solace.mh.se!umdac!cs.umu.se!dvlgsd
From: dvlgsd@cs.umu.se (Gunnar Strand)
Subject: Re: Solution: find the path algorithm
Message-ID: <D667Hy.KEK@cs.umu.se>
Sender: news@cs.umu.se (News Administrator)
Organization: Dept. of Computing Science, Umea Univ., 901 87  Umea, Sweden
References: <3krkml$gdo@infoserv.rug.ac.be> <3kt4oq$9kl@gap.cco.caltech.edu>
Date: Tue, 28 Mar 1995 21:53:55 GMT
Lines: 68


Ok, I haven't followed this thread from the start, but the question
seem to be to find the (shortest?) path in a maze of some sort. All
solutions I've seen so far is depth-first, but I'd recommend a
bredth-first algorithm instead, if it's fair to say that the maze
is somewhat "obstacled". Apologize if this one already has been up
for discussion.

1  Take start coordinate, tag it and put it first in a queue.
2  While the queue is not empty:
3    Pop first coordinate off the queue
4    For all directions from the coordinate [from step 2]:
5      Take new coordinate [in direction]
6      If the new coordinate is tagged
7        Next
8      Else If the new coordinate is destination
9        Return path to the new coordinate
10     Else
11       Tag the new coordinate and put it last in the queue
12   Next
13 Endwhile
14 Return No path to destination exists

The algorithm will expand and "fill out" the (entire) maze by taking
one step in all directions, always finding the shortest path. It will
also detect if no path exists, e.g if the destination is in a closed
room. It's also easy to weight the coordinates, adding a new weight as
you take a step, making it ideal for troop movement which someone
mentioned in a previous posting.

This will suck more memory than a depth-first, since you need to
store the path for each element on the edge of a circle. Maximum memory
needed at any one time is about 2*pi*R * 2*pi*R^2 * sizeof(coord)^2
(if I calculated correctly 8-). But in a narrow maze the cost will be
considerably less.

Here's an example, assuming only four directions are allowed:

S - Start, G - Goal, X = Wall, . - Tagged

0             1             2             3             4
------------  ------------  ------------  ------------  ------------  
|     DX   |  |     DX   |  |     DX   |  |     DX . |  |     DX...|  
|X XXXXXX X|  |X XXXXXX X|  |X XXXXXX.X|  |X XXXXXX.X|  |X XXXXXX.X|  
|    X  S  |  |    X .S. |  |    X..S..|  |    X..S..|  |    X..S..|  
| XX   XXX |  | XX   XXX |  | XX   XXX |  | XX  .XXX.|  | XX ..XXX.|  
|   XXX    |  |   XXX    |  |   XXX    |  |   XXX    |  |   XXX   .|  
------------  ------------  ------------  ------------  ------------  

5             6             7             8             9
------------  ------------  ------------  ------------  ------------  
|     DX...|  |     DX...|  |     DX...|  |     DX...|  |     DX...|  
|X XXXXXX.X|  |X XXXXXX.X|  |X XXXXXX.X|  |X XXXXXX.X|  |X XXXXXX.X|  
|    X..S..|  |   .X..S..|  |   .X..S..|  |  ..X..S..|  | ...X..S..|  
| XX...XXX.|  | XX...XXX.|  | XX...XXX.|  | XX...XXX.|  | XX...XXX.|  
|   XXX  ..|  |   XXX ...|  |   XXX....|  |   XXX....|  |   XXX....|  
------------  ------------  ------------  ------------  ------------  

I leave the rest up to you as an exercise 8-).

Luck,

Gunnar
--
Gunnar Strand, dvlgsd@cs.umu.se           www: http://www.cs.umu.se/~dvlgsd
Phone: +46 090 19 71 75         Low-tech mail: Fysikgrnd 29A:4 907 31 Ume
____________________________________________________________________________
DISCLAIMER: I've got the worst case of dyslexia since Beethoven whent blind.
