Finding path between 2 points in Racket

前端 未结 2 1915
天命终不由人
天命终不由人 2020-12-21 18:48

I have following list of connections:

(define routelist
  (list
      (list\'a \'b)
      (list\'a \'c)
      (list\'b \'e)
      (list\'b \'f)
      (list\         


        
2条回答
  •  [愿得一人]
    2020-12-21 19:36

    Use DFS algorithm will be ok.

    (define (mainpath routelist start end)
      (letrec ([next-nodes (λ (node)
                            (for/list ([al routelist]
                                       #:when (eq? node (first al)))
                                      (second al)))]
               [path (λ (node vlist)
                       (let ([new-list (cons node vlist)])
                         (when (eq? node end)
                           (println (reverse new-list)))
                         (for ([next (next-nodes node)]
                               #:unless (memq next vlist))
                              (path next new-list))))])
        (path start '())))
    

提交回复
热议问题