灌装LISP的水罐

时间:2015-08-12 09:27:50

标签: lisp common-lisp water-jug-problem

从一个空的5加仑水壶和一个空的11加仑水壶开始,我们怎样才能在11加仑的水壶和5加仑的水壶中装满3加仑的水呢?

我想在Lisp中编写一个函数来计算这个难题中任何状态的后继状态列表

我的解决方案

var express = require('express');
var router = express.Router();

var news = [{id: 1, title: 'News title'}];

router.get('/', function(req, res, next) {
  res.render('index', { title: 'Main page', news: news });
});

module.exports = router;

如何实施(0 0) > (5 0) > (0 5) > (5 5) > (0 10 ) > (5 10)>(4 11)>(4 0)>(0 4)>(5 4)>(0 9)>(5 9)>(3 11)>(3 0)>(0 3) 功能?

successors

请帮助!!!!

1 个答案:

答案 0 :(得分:1)

这是一个开始的提示:

(defun successors (state)        ; for each state
  (let ((jug1 (first state))     ; gallons in jug1 for state
        (jug2 (second state))    ; gallons in jug2 for state
        (new-states nil))        ; successor states of state
    (when (< jug1 5)             ; if jug1 is not full
      (push (list 5 jug2) new-states))  ; then fill jug1
    ; do the same for jug2
    ; ...
    (when (> jug1 0)             ; if jug1 has some water
      ;...                         empty jug1, that is, new-state = (0 jug2)
    ; do the same for jug2 if jug2 has some water
    ;...
    (when (and (> jug2 0) (< jug1 5)) ; if jug2 can give water to jug1
      ; then pour the water of jug2 in jug1
      (push (list (min 5 (+ jug1 jug2))
                  (max (- jug2 (- 5 jug1)) 0)) new-states))
    ; do the same for the opposite situation
    ;...
    new-states))              ; finally return the set of new states