Using Lisp, implement a program that solves the Missionaries and Cannibals problem that uses a DFS( depth first search). The function call should use (mac start(this is the current state of the problem) end(this is the desired state)). So the problem should output sequences of moves to go from the start state to the desired state! For example, the call should be like: Call: (mac '(3 3 l) '(0 0 r)) Output: ((3 3 L) (2 2 R) (3 2 L) (3 0 R) (3 1 L) (1 1 R) (2 2 L) (0 2 R) (0 3 L) (0 1 R) (1 1 L) (0 0 R))

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Using Lisp, implement a program that solves the Missionaries and Cannibals problem that uses a DFS( depth first search). The function call should use (mac start(this is the current state of the problem) end(this is the desired state)). So the problem should output sequences of moves to go from the start state to the desired state!

For example, the call should be like:

Call: (mac '(3 3 l) '(0 0 r))

Output: ((3 3 L) (2 2 R) (3 2 L) (3 0 R) (3 1 L) (1 1 R) (2 2 L) (0 2 R) (0 3 L) (0 1 R) (1 1 L) (0 0 R))

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

(defmacro mac (start end)

  `(dfs ,start ,end nil nil))

 

(defun dfs (current-state goal-state path &optional (solution-found nil))

  (cond

    ((and solution-found (equal current-state goal-state))

     (format t "Solution: ~A~%" (reverse path)))

    ((member current-state path)

     nil)

    (t

     (let ((new-paths (generate-next-states current-state)))

       (dolist (new-state new-paths)

         (dfs new-state goal-state (cons current-state path) t))))))

 

(defun generate-next-states (state)

  (let* ((m (car state))

         (c (cadr state))

         (b (caddr state))

         (next-states '()))

    (loop for m-move from 0 to m

          do (loop for c-move from 0 to c

                   do (when (valid-move m-move c-move m c b)

                        (let* ((new-state (update-state state m-move c-move))

                               (missionaries-on-left (- m m-move))

                               (cannibals-on-left (- c c-move))

                               (boat-position (if (eq b 'l) 'r 'l)))

                          (push (list missionaries-on-left cannibals-on-left boat-position) next-states)))))

    next-states))

 

(defun valid-move (m-move c-move m c b)

  (and (<= 0 m-move m) (<= 0 c-move c)

       (<= (+ m-move c-move) 2)

       (or (and (eq b 'l) (>= (- m m-move) (- c c-move)))

           (and (eq b 'r) (>= (- c c-move) (- m m-move))))))

 

(defun update-state (state m-move c-move)

  (let* ((m (car state))

         (c (cadr state))

         (b (caddr state)))

    (if (eq b 'l)

        (list (- m m-move) (- c c-move) 'r)

        (list (+ m m-move) (+ c c-move) 'l))))

 

;; Example usage:

(mac '(3 3 l) '(0 0 r))

This is the code provided that runs, but gives the incorrect output. Check the attached image to see what I get. 

The solution should simply be:

((3 3 L) (2 2 R) (3 2 L) (3 0 R) (3 1 L) (1 1 R) (2 2 L) (0 2 R) (0 3 L) (0 1 R) (1 1 L) (0 0 R))

Please help me fix!

Solution:
((3 3 L) (2 2 R) (1 1 L) (1 1 R) (1 1 L)
(1 1 L) (1 1 R)
(1 1 R)
(1 1 L)
(1 1 R)
(1 1 L)
(1 1 L) (1 1 R)
(1 1 L) (1 1
(1 1 L) (1 1 R)
(1 1 L)
(1 1 L)
R) (1 1 L)
(1 1 L)
(1 1 R)
(1 1 R)
(1 1 L)
(1 1 L)
(1 1 L)
(1 1 L)
(1 1 L) (1 1 R)
(1 1 L)
(1 1 R)
(1 1 L) (1 1 R)
(1 1 L)
(1 1 R)
(1 1 L) (1 1 R) (1 1 L)
(1 1 R)
(1 1 L)
(1 1 L) (1 1 R)
(1 1 R)
(1 1 L)
(1 1 R)
(1 1
L)
(1 1 L)
(1 1 L)
(1 1 L)
(1 1
(1 1
R)
(1 1
L)
L)
(1 1 R)
(1 1
L)
(1 1 L)
(1 1 R)
(1 1
L)
(1 1 L) (1 1 R)
(1 1
L)
(1 1 L) (1 1 R)
(1 1 L) (1 1 R)
(1 1 L) (1 1 R)
(1 1 L)
(1 1 R)
(1 1 R)
(1 1 R)
(1 1 L)
(1 1 L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1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 (1 1 R) (1 1 L) L) (1 1 R) (1 1 L) (1 1 L) (1 1 R) (1 1 L) (1 1 L) (1 1 R) (1 1 L) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 R) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 L) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 L) (1 1 R) (1 1 L) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 L) (1 1 R) (1 1 L) (1 1 L) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 L) (1 1 R) (1 1 L) (1 1 L) (1 1 L) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 L) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 R) (1 1 L) (1 1 R) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) L) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R) (1 1 L) (1 1 R)
Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

The code still does not seem to work for me. The error I get is: "Program stack overflow." I am using GNU Common Lisp Listener, perhaps this code can be tweaked again to ensure this error does not pop up again?

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

This code is still not working, can you fix it? I get the same error: "L is not a number". 

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

I have tried to run the code, using the example usage as well and I get the error: "L is not a number".

I will note that I am using GNU Common Lisp to run the code. Do you know how I could fix that?

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Fibonacci algorithm
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education