|
* Missionaries and Cannibals
|
бет | 10/10 | Дата | 07.02.2022 | өлшемі | 38,44 Kb. | | #84826 | түрі | Программа |
| Байланысты: Каннибал мен миссионерлер* Missionaries and Cannibals
$ENTRY Go { =
; };
Search {
*1. The goal found
e1 (s.M'='R()('MMMCCC')) =
;
*2. Impossible state. No backup. No solution.
(0'='eS) (5'='Imposs) =
;
*3. Impossible state. No next move. Back up.
e1 (s.Mp'='eS) (5'='Imposs) =
;
*4. Impossible state. Do next move.
e1 (s.Mp'='eS)(s.M'='Imposs),
<+ s.M 1>: s.Mn =
(s.Mn'=')>;
*5. Repeated state. Equate to impossible.
e1 (s.Mp'='eS) e2 (s.M'='eS) =
;
*6. Down the tree.
e1 (s.M'='eS) =
)>;
}
* Move
Move { eS = >; }
* Try a move
Try {
* Boat on left bank
* MM crossing
1 L('MM'e1)(e2) = R(e1)('MM'e2);
* CC crossing
2 L(e1'CC')(e2) = R(e1)(e2'CC');
* MC crossing
3 L(e1'MC'e2)(e3) = R(e1 e2)('M'e3'C');
* M crossing
4 L('M'e1)(e2) = R(e1)('M'e2);
* C crossing
5 L(e1'C')(e2) = R(e1)(e2'C');
* Boat on right bank
* MM crossing
1 R(e1)('MM'e2) = L('MM'e1)(e2);
* CC crossing
2 R(e1)(e2'CC') = L(e1'CC')(e2);
* MC crossing
3 R(e1)('M'e2'C') = L('M'e1'C')(e2);
* M crossing
4 R(e1)('M'e2) = L('M'e1)(e2);
* C crossing
5 R(e1)(e2'C') = L(e1'C')(e2);
* Otherwise move impossible
s.M eS = Imposs;
}
* Admissibility of the move
Admiss {
s.Side(eL)(eR), :T, :T =
s.Side(eL)(eR);
eS = Imposs;
}
* No eating missionaries
Noeat {
*1. Both missionaries and cannibals are present
'M'e1'C'= ;
* Otherwise OK
e1 = T; }
* Both M and C on the bank.Compare the numbers.
Compare {
'C'e1 = F;
'M'e1'C'= ;
e1 = T; }
* Print the path leading to the goal
Path {
(0'='eS) e2 =
;
(s.M'='s.Side eS) e2,
>: e.Who =
' bank: state ' s.Side eS>
;
= ;
}
Look-up { sM In e1 sM(e.Who) e2 = e.Who }
Table { =
1('MM') 2('CC') 3('MC') 4('M ') 5('C ') }
Назовем файл этой программы mmmccc.ref . Транслируем его посредством refc и запускаем:
refgo mmmccc -nt
(указанные флажки приводят к выдаче на печать номера шага РЕФАЛ-машины и затраченного на шаг времени). Вывод имеет вид:
The initial state: L (MMMCCC)()
CC crossing to R bank: state R (MMMC)(CC)
C crossing to L bank: state L (MMMCC)(C)
CC crossing to R bank: state R (MMM)(CCC)
C crossing to L bank: state L (MMMC)(CC)
MM crossing to R bank: state R (MC)(MMCC)
MC crossing to L bank: state L (MMCC)(MC)
MM crossing to R bank: state R (CC)(MMMC)
C crossing to L bank: state L (CCC)(MMM)
CC crossing to R bank: state R (C)(MMMCC)
M crossing to L bank: state L (MC)(MMCC)
MC crossing to R bank: state R ()(MMMCCC) | Достарыңызбен бөлісу: |