This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Labyrinth:how to find the path?

7 posts · 6721 views

I have a list of the rooms(e.x. ((1 2) (2 4) (IN 1) (IN 3) (1 3) (4 5) (5 OUT)),where () means that rooms are connected with each other "IN"-entrance,"OUT"-exit).Would you be so kind to explaine the algorithm ,which can find the exit ?

Last edited by terance on , edited 1 time in total.

Re: Labyrinth:how to find the exit?

yep you are right ....I need a path ...
Could you explaine how to build from this list:((1 2) (2 4) (IN 1) (IN 3) (1 3) (4 5) (5 OUT)) correct graph?

Re: Labyrinth:how to find the exit?

terance wrote:yep you are right ....I need a path ...
Could you explaine how to build from this list:((1 2) (2 4) (IN 1) (IN 3) (1 3) (4 5) (5 OUT)) correct graph?
In this list you have all the edges of a graph. So this list one of the ways to represent the graph. When you do a breadth-first search, the only thing that needs to be known about the graph is the start vertex (IN), goal (OUT vertex), and the way to enumerate all vertices that are connected to the current one. So, you just need to implement a function that will search which vertices are connected to the given one and plug it into the search algorithm. This function can operate directly on this list or it can use the array/hashtable that contains the mapping from vertices to adjacent vertices.

Re: Labyrinth:how to find the path?

I've wote something .It's working, but I'd like to know how to re-write this code without using
 setq
?
(defun search_room (point labirint)                                                                              
  (cond ((null labirint) nil)                                                                                         
           ((eql (caar labirint) point) (second (car labirint)))                                                         
           (t (search_room point (cdr labirint)))))
(setq way '(in))
(defun search_path (labirint)                                                                                        
  (setq way (cons (search_room(first way) labirint) way))   ;(push (search_room(first  *way-out*) labirint)  *way-out*)                                                      
     (cons (eql (car way) 'out)                                                                                    
         (t (reverse way)))     ;переварачивает а потом разрушает список way                                                                                       
         (search_path labirint))

Re: Labyrinth:how to find the path?

terance wrote:I've wote something .It's working, but I'd like to know how to re-write this code without using
 setq
?
(defun search_room (point labirint)                                                                              
  (cond ((null labirint) nil)                                                                                         
           ((eql (caar labirint) point) (second (car labirint)))                                                         
           (t (search_room point (cdr labirint)))))
(setq way '(in))
(defun search_path (labirint)                                                                                        
  (setq way (cons (search_room(first way) labirint) way))   ;(push (search_room(first  *way-out*) labirint)  *way-out*)                                                      
     (cons (eql (car way) 'out)                                                                                    
         (t (reverse way)))     ;переварачивает а потом разрушает список way                                                                                       
         (search_path labirint))
Very minimal: For your second function, you could transform:
(setq way '(in))
(defun ...)
to
(let ((way '(in)))
  (defun ...))
Then use setf instead of setq inside (really no difference; setf will expand to setq here).

Re: Labyrinth:how to find the path?

Hi to everyone!I have problems with my code:it doesn't detect cycles in the labyrinth...Any ideas about how to detect cycles?
My idea is that when romm meets 2 times in the variable PATH program should write:"Cycle exists!"