Dinesman's multiple-dwelling problem: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) m →{{header|C sharp}}: Regularize header markup to recommended on category page |
|||
Line 2,726: | Line 2,726: | ||
<!--</lang>--> |
<!--</lang>--> |
||
Same output |
Same output |
||
=={{header|Picat}}== |
|||
First a constraint modelling version (using Picat's cp module), then a version using permutations. |
|||
<lang picat>import util. |
|||
import cp. |
|||
go => |
|||
dinesman_cp, |
|||
dinesman2, |
|||
nl. |
|||
% CP approach |
|||
dinesman_cp => |
|||
println(dinesman_cp), |
|||
N = 5, |
|||
X = [Baker, Cooper, Fletcher, Miller, Smith], |
|||
X :: 1..N, |
|||
all_different(X), |
|||
% Baker does not live on the fifth floor. |
|||
Baker #!= 5, |
|||
% Cooper does not live on the first floor. |
|||
Cooper #!= 1, |
|||
% Fletcher does not live on either the fifth or the first floor. |
|||
Fletcher #!= 5, |
|||
Fletcher #!= 1, |
|||
% Miller lives on a higher floor than does Cooper. |
|||
Miller #> Cooper, |
|||
% Smith does not live on a floor adjacent to Fletcher'. |
|||
abs(Smith-Fletcher) #> 1, |
|||
% Fletcher does not live on a floor adjacent to Cooper's. |
|||
abs(Fletcher-Cooper) #> 1, |
|||
solve(X), |
|||
println([baker=Baker, cooper=Cooper, fletcher=Fletcher, miller=Miller, smith=Smith]). |
|||
% |
|||
% The constraints (non CP approach) |
|||
% |
|||
% floors: 1: bottom .. 5: top floor |
|||
% |
|||
constraints([B,C,F,M,S]) => |
|||
B != 5, % Baker not top floor |
|||
C != 1, % Cooper not bottom floor |
|||
F != 1, F != 5, % Fletcher not botton nor top floor |
|||
M > C, % Miller higher floor than Cooper |
|||
not adjacent(S, F), % Smith and Fletcher not adjacent |
|||
not adjacent(F, C). % Fletcher and Cooper not adjacent |
|||
adjacent(A,B) => abs(A-B) == 1. |
|||
% Non-CP approach, using permutations |
|||
dinesman2 => |
|||
println(dinesman2), |
|||
foreach([B,C,F,M,S] in permutations(1..5), constraints([B,C,F,M,S])) |
|||
println([baker=B, cooper=C, fletcher=F, miller=M, smith=S]) |
|||
end. |
|||
</lang> |
|||
Output: |
|||
<pre> |
|||
dinesman_cp |
|||
[baker = 3,cooper = 2,fletcher = 4,miller = 5,smith = 1] |
|||
dinesman1 |
|||
[baker = 3,cooper = 2,fletcher = 4,miller = 5,smith = 1] |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |