fork download
  1. (defstruct estado
  2. ml cl ; misioneros y caníbales en la izquierda
  3. mr cr ; misioneros y caníbales en la derecha
  4. side ; 0 = bote a la izquierda, 1 = derecha
  5. camino) ; lista de pasos
  6.  
  7. (defun estado-valido-p (st)
  8. (let ((ml (estado-ml st))
  9. (cl (estado-cl st))
  10. (mr (estado-mr st))
  11. (cr (estado-cr st)))
  12. (and (>= ml 0) (<= ml 3)
  13. (>= cl 0) (<= cl 3)
  14. (>= mr 0) (<= mr 3)
  15. (>= cr 0) (<= cr 3)
  16. (or (= ml 0) (>= ml cl))
  17. (or (= mr 0) (>= mr cr)))))
  18.  
  19. (defun mover (st m c)
  20. (let* ((ml (estado-ml st))
  21. (cl (estado-cl st))
  22. (mr (estado-mr st))
  23. (cr (estado-cr st))
  24. (side (estado-side st)))
  25. (cond
  26. ((= side 0)
  27. (make-estado :ml (- ml m) :cl (- cl c)
  28. :mr (+ mr m) :cr (+ cr c)
  29. :side 1
  30. :camino (append (estado-camino st)
  31. (list (format nil "~dM~dC ->" m c)))))
  32. (t
  33. (make-estado :ml (+ ml m) :cl (+ cl c)
  34. :mr (- mr m) :cr (- cr c)
  35. :side 0
  36. :camino (append (estado-camino st)
  37. (list (format nil "~dM~dC <-" m c))))))))
  38.  
  39. (defun iguales (a b)
  40. (and (= (estado-ml a) (estado-ml b))
  41. (= (estado-cl a) (estado-cl b))
  42. (= (estado-mr a) (estado-mr b))
  43. (= (estado-cr a) (estado-cr b))
  44. (= (estado-side a) (estado-side b))))
  45.  
  46. (defun repetido-p (nuevo camino)
  47. (some (lambda (e) (iguales e nuevo)) camino))
  48.  
  49. (defparameter *viajes*
  50. '((2 0) (0 2) (1 1) (1 0) (0 1)))
  51.  
  52. (defun resolver (estado final camino)
  53. (when (iguales estado final)
  54. (push estado camino)
  55. (mostrar-solucion (reverse camino))
  56. (return-from resolver))
  57.  
  58. (dolist (v *viajes*)
  59. (let ((m (first v))
  60. (c (second v)))
  61. (when (and (<= (+ m c) 2) (> (+ m c) 0))
  62. (let ((nuevo (mover estado m c)))
  63. (when (and (estado-valido-p nuevo)
  64. (not (repetido-p nuevo camino)))
  65. (resolver nuevo final (cons estado camino))))))))
  66.  
  67. (defun personas (m c)
  68. (concatenate 'string
  69. (make-string m :initial-element #\M)
  70. (make-string c :initial-element #\C)))
  71.  
  72. (defun lado-a-string (m c)
  73. (format nil "[~a]" (personas m c)))
  74.  
  75. (defun mostrar-paso (anterior actual)
  76. (let ((ml (estado-ml anterior))
  77. (cl (estado-cl anterior))
  78. (mr (estado-mr anterior))
  79. (cr (estado-cr anterior))
  80. (side (estado-side anterior))
  81. (ml2 (estado-ml actual))
  82. (cl2 (estado-cl actual))
  83. (mr2 (estado-mr actual))
  84. (cr2 (estado-cr actual))
  85. (side2 (estado-side actual)))
  86.  
  87. ;; Determinar quién cruzó
  88. (let* ((dm (- ml ml2))
  89. (dc (- cl cl2))
  90. (m (abs dm))
  91. (c (abs dc))
  92. (cruce (personas m c)))
  93. (format t "~%Paso ~a (antes de cruzar):~%" (+ 1 (length (estado-camino anterior))))
  94. (if (= side 0)
  95. (format t "~a B ~~~~~~ []~%" (lado-a-string ml cl))
  96. (format t "~a ~~~~~~ B ~a~%" (lado-a-string ml cl) (lado-a-string mr cr)))
  97. (format t "Cruzando con: ~a~%" cruce)
  98. (format t "Paso ~a (después de cruzar):~%" (+ 1 (length (estado-camino anterior))))
  99. (if (= side2 0)
  100. (format t "~a B ~~~~~~ []~%" (lado-a-string ml2 cl2))
  101. (format t "~a ~~~~~~ B ~a~%" (lado-a-string ml2 cl2) (lado-a-string mr2 cr2))))))
  102.  
  103. (defun mostrar-solucion (camino)
  104. (format t "~%====================================~%") ;traviesos
  105. (format t "Solución en ~d pasos~%" (length (estado-camino (car (last camino)))))
  106. (loop for (a b) on camino while b do
  107. (mostrar-paso a b))
  108. (format t "~%====================================~%"))
  109.  
  110. (defun main ()
  111. (let ((inicio (make-estado :ml 3 :cl 3 :mr 0 :cr 0 :side 0 :camino '()))
  112. (fin (make-estado :ml 0 :cl 0 :mr 3 :cr 3 :side 1 :camino '())))
  113. (resolver inicio fin '())))
Success #stdin #stdout #stderr 0.01s 9608KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Warning: reserving address range 0x80000c0000...0x1fffffffffff that contains memory mappings. clisp might crash later!
Memory dump:
  0x8000000000 - 0x80000bffff
  0x149195200000 - 0x1491954e4fff
  0x149195615000 - 0x149195639fff
  0x14919563a000 - 0x1491957acfff
  0x1491957ad000 - 0x1491957f5fff
  0x1491957f6000 - 0x1491957f8fff
  0x1491957f9000 - 0x1491957fbfff
  0x1491957fc000 - 0x1491957fffff
  0x149195800000 - 0x149195802fff
  0x149195803000 - 0x149195a01fff
  0x149195a02000 - 0x149195a02fff
  0x149195a03000 - 0x149195a03fff
  0x149195a80000 - 0x149195a8ffff
  0x149195a90000 - 0x149195ac3fff
  0x149195ac4000 - 0x149195bfafff
  0x149195bfb000 - 0x149195bfbfff
  0x149195bfc000 - 0x149195bfefff
  0x149195bff000 - 0x149195bfffff
  0x149195c00000 - 0x149195c03fff
  0x149195c04000 - 0x149195e03fff
  0x149195e04000 - 0x149195e04fff
  0x149195e05000 - 0x149195e05fff
  0x149195f3d000 - 0x149195f40fff
  0x149195f41000 - 0x149195f41fff
  0x149195f42000 - 0x149195f43fff
  0x149195f44000 - 0x149195f44fff
  0x149195f45000 - 0x149195f45fff
  0x149195f46000 - 0x149195f46fff
  0x149195f47000 - 0x149195f54fff
  0x149195f55000 - 0x149195f62fff
  0x149195f63000 - 0x149195f6ffff
  0x149195f70000 - 0x149195f73fff
  0x149195f74000 - 0x149195f74fff
  0x149195f75000 - 0x149195f75fff
  0x149195f76000 - 0x149195f7bfff
  0x149195f7c000 - 0x149195f7dfff
  0x149195f7e000 - 0x149195f7efff
  0x149195f7f000 - 0x149195f7ffff
  0x149195f80000 - 0x149195f80fff
  0x149195f81000 - 0x149195faefff
  0x149195faf000 - 0x149195fbdfff
  0x149195fbe000 - 0x149196063fff
  0x149196064000 - 0x1491960fafff
  0x1491960fb000 - 0x1491960fbfff
  0x1491960fc000 - 0x1491960fcfff
  0x1491960fd000 - 0x149196110fff
  0x149196111000 - 0x149196138fff
  0x149196139000 - 0x149196142fff
  0x149196143000 - 0x149196144fff
  0x149196145000 - 0x14919614afff
  0x14919614b000 - 0x14919614dfff
  0x149196150000 - 0x149196150fff
  0x149196151000 - 0x149196151fff
  0x149196152000 - 0x149196152fff
  0x149196153000 - 0x149196153fff
  0x149196154000 - 0x149196154fff
  0x149196155000 - 0x14919615bfff
  0x14919615c000 - 0x14919615efff
  0x14919615f000 - 0x14919615ffff
  0x149196160000 - 0x149196180fff
  0x149196181000 - 0x149196188fff
  0x149196189000 - 0x149196189fff
  0x14919618a000 - 0x14919618afff
  0x14919618b000 - 0x14919618bfff
  0x561e19419000 - 0x561e19509fff
  0x561e1950a000 - 0x561e19613fff
  0x561e19614000 - 0x561e19673fff
  0x561e19675000 - 0x561e196a3fff
  0x561e196a4000 - 0x561e196d4fff
  0x561e196d5000 - 0x561e196d8fff
  0x561e1a842000 - 0x561e1a862fff
  0x7fffc8235000 - 0x7fffc8255fff
  0x7fffc833b000 - 0x7fffc833efff
  0x7fffc833f000 - 0x7fffc8340fff