Advertisement
zbeucler

grid-DFS.ss

Feb 22nd, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. (define path-lst '())
  2.  
  3. (define expand
  4. (lambda (point)
  5. (let* ((lst0 (adjacentv point))
  6. (lst (randomize lst0)))
  7. (set-lst-frontier lst)
  8. (add-to-path-lst lst point)
  9. (push lst))))
  10.  
  11. (define randomize
  12. (lambda (lst)
  13. (let ((size (length lst)))
  14. (cond
  15. ((< size 2) lst)
  16. (else
  17. (let* ((node-num (random size))
  18. (nodex (list-ref lst node-num))
  19. (new-lst (removex node-num lst)))
  20. (cons nodex (randomize new-lst))))))))
  21.  
  22. (define removex
  23. (lambda (num lst)
  24. (if (= num 0)
  25. (cdr lst)
  26. (cons (car lst) (removex (- num 1) (cdr lst))))))
  27.  
  28. (define add-to-path-lst
  29. (lambda (lst point)
  30. (if (not (null? lst))
  31. (let ((child-parent (list (car lst) point)))
  32. (set! path-lst (cons child-parent path-lst))
  33. (add-to-path-lst (cdr lst) point)))))
  34.  
  35. (define set-lst-frontier
  36. (lambda (lst)
  37. (if (null? lst)
  38. '()
  39. (let ((x (car lst)))
  40. (draw-pt-frontier x)
  41. (block-set! x frontier)
  42. (set-lst-frontier (cdr lst))))))
  43.  
  44. (define draw-pt-frontier
  45. (lambda (pt)
  46. (draw-frontier (car pt) (cadr pt))))
  47.  
  48. (define search
  49. (lambda (grid stop-count)
  50. (block-set! start frontier)
  51. (set! path-lst (list (list start '())))
  52. (search2 grid 1 stop-count)))
  53.  
  54. (define search2
  55. (lambda (grid count stop-count)
  56. ;(display stack) (newline)
  57. (pause pause-num)
  58. (display count)
  59. (newline)
  60. (expand robot)
  61. (let ((next-robot (top)))
  62. (cond
  63. ((null? next-robot)
  64. (display "Cannot reach the goal")
  65. (newline))
  66. ((equal? next-robot goal)
  67. (set! robot (pop))
  68. (draw-moved-robot (robot-x) (robot-y))
  69. (display "Found")
  70. (newline)
  71. (let ((path (get-path goal)))
  72. (draw-path path)
  73. (display path))
  74. (newline))
  75. ((>= count stop-count)
  76. (display "Took too long")
  77. (newline))
  78. (else
  79. (draw-visited (car robot) (cadr robot))
  80. (set! robot (pop))
  81. (draw-moved-robot (robot-x) (robot-y))
  82. (search2 grid (+ count 1) stop-count))))))
  83.  
  84. (define get-path
  85. (lambda (last-node)
  86. (if (equal? last-node start)
  87. (list start)
  88. (let ((next-node (cadr (assoc last-node path-lst))))
  89. (append (get-path next-node) (list last-node))))))
  90.  
  91. (define draw-path
  92. (lambda (path)
  93. (cond
  94. ((not (null? path))
  95. (draw-pt-path-node (car path))
  96. (draw-path (cdr path))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement