my code of missionaries and cannibals
Annie <[email protected]> Wed, 30 Apr 2003 20:52:23 -0700 (PDT)
| Newsgroups | gmane.lisp.corman |
|---|---|
| Message-ID | <[email protected]> |
helloi m sending a small portion of my code for missionaries and cannibals in lisp....my code determines only one path from start to goal state...can anyone help me to write a code in lisp which determines all the possible paths to the goal state.reply soon.thanksbye.
>(setf start '(3 3 1 0 0 0))
(3 3 1 0 0 0)
>(setf finish '(0 0 0 3 3 1))
(0 0 0 3 3 1)
>(setf all-actions '((1 0 1) (0 1 1) (2 0 1) (0 2 1) (1 1 1)))
((1 0 1) (0 1 1) (2 0 1) (0 2 1) (1 1 1))
>(defun mcp (start finish &optional
(queue (list (list start))))
(cond ((endp queue) nil)
((equal finish (first (first queue)))
(reverse (first queue)))
(t (mcp start finish
(append (tree (first queue))
(rest queue))))))
MCP
>(defun tree (path)
(print (reverse path))
(setf nodes (get-nodes path))
(mapcar #'(lambda (new-node) (cons new-node path))
(filter-nodes nodes path)))
TREE
>(defun get-nodes (path)
(if (= 1 (third (first path)))
(first-approach (first path) all-actions)
(second-approach (first path) all-actions)))
GET-NODES
>(defun filter-nodes (nodes path)
(cond ((endp nodes) nil)
((and (not (endp (first nodes)))
(not (member (first nodes) path :test #'equal)))
(cons (first nodes)
(filter-nodes (rest nodes) path)))
(t (filter-nodes (rest nodes) path))))
FILTER-NODES
Output:
((3 3 1 0 0 0) (2 2 0 1 1 1) (3 2 1 0 1 0) (3 0 0 0 3 1) (3 1 1 0 2 0)
(1 1 0 2 2 1) (2 2 1 1 1 0) (0 2 0 3 1 1) (0 3 1 3 0 0) (0 1 0 3 2 1)
(0 2 1 3 1 0) (0 0 0 3 3 1))
---------------------------------
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.