Jump to content

Dinesman's multiple-dwelling problem: Difference between revisions

Added Ada
(Added Ada)
Line 8:
;The problem:
:''Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that contains only five floors. Baker does not live on the top floor. Cooper does not live on the bottom floor. Fletcher does not live on either the top or the bottom floor. Miller lives on a higher floor than does Cooper. Smith does not live on a floor adjacent to Fletcher's. Fletcher does not live on a floor adjacent to Cooper's. Where does everyone live?''
 
=={{header|Ada}}==
Uses an enum type People to attempt to be naturally reading. Problem is easily changed by altering subtype Floor, type people and the somewhat naturally reading constraints in the Constrained function. If for example you change the floor range to 1..6 and add Superman to people, all possible solutions will be printed.
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
procedure Dinesman is
subtype Floor is Positive range 1 .. 5;
type People is (Baker, Cooper, Fletcher, Miller, Smith);
type Floors is array (People'Range) of Floor;
type PtFloors is access all Floors;
function Constrained (f : PtFloors) return Boolean is begin
if f (Baker) /= Floor'Last and
f (Cooper) /= Floor'First and
Floor'First < f (Fletcher) and f (Fletcher) < Floor'Last and
f (Miller) > f (Cooper) and
abs (f (Smith) - f (Fletcher)) /= 1 and
abs (f (Fletcher) - f (Cooper)) /= 1
then return True; end if;
return False;
end Constrained;
procedure Solve (list : PtFloors; n : Natural) is
procedure Swap (I : People; J : Natural) is
temp : constant Floor := list (People'Val (J));
begin list (People'Val (J)) := list (I); list (I) := temp;
end Swap;
begin
if n = 1 then
if Constrained (list) then
for p in People'Range loop
Put_Line (p'Img & " on floor " & list (p)'Img);
end loop;
end if;
return;
end if;
for i in People'First .. People'Val (n - 1) loop
Solve (list, n - 1);
if n mod 2 = 1 then Swap (People'First, n - 1);
else Swap (i, n - 1); end if;
end loop;
end Solve;
thefloors : aliased Floors;
begin
for person in People'Range loop
thefloors (person) := People'Pos (person) + Floor'First;
end loop;
Solve (thefloors'Access, Floors'Length);
end Dinesman;</lang>
{{out}}
<pre>BAKER on floor 3
COOPER on floor 2
FLETCHER on floor 4
MILLER on floor 5
SMITH on floor 1</pre>
 
=={{header|Bracmat}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.