كد:
unit HV;
interface
const
Left=false;
Right=true;
Human=0;
Vampire=1;
Type
TPopulation=array[Human..Vampire] of byte;
TRiverCondition=Record
SidesPopulation:array[Left..Right] of TPopulation;
BoatSide:Boolean;
end;
var
n:byte;
FirstSide:boolean;
function Transport(a:TRiverCondition):Boolean;
implementation
function IsValid(a:TRiverCondition):boolean;
begin
Result:=(a.SidesPopulation[Left][Human]>0)and
(a.SidesPopulation[Left][Vampire]>0)and
(a.SidesPopulation[Right][Human]>0)and
(a.SidesPopulation[Right][Vampire]>0)and
(a.SidesPopulation[Left][Human]>=
a.SidesPopulation[Left][Vampire]) and
(a.SidesPopulation[Right][Human]>=
a.SidesPopulation[Right][Vampire]);
end;
function Goal(a:TRiverCondition):boolean;
begin
Result:=(a.SidesPopulation[FirstSide][Human]=0)and
(a.SidesPopulation[FirstSide][Vampire]=0)and
(a.SidesPopulation[not FirstSide][Human]=n)and
(a.SidesPopulation[not FirstSide][Vampire]=n);
end;
procedure WriteCondition(a:TRiverCondition);
begin
//display a in somewhere
end;
function Transport(a:TRiverCondition):Boolean;
var
vv,hh:byte;
ok:Boolean;
b:TRiverCondition;
begin
b.BoatSide:=not a.BoatSide;
ok:=Goal(a);
if IsValid(a) and not ok then
for vv:=0 to 2 do
for hh:=0 to 2 do
if ((vv+hh)>0) and (not ok) then begin
b.SidesPopulation[a.BoatSide][Human]:=a.SidesPopulation[a.BoatSide][Human]-hh;
b.SidesPopulation[not a.BoatSide][Human]:=a.SidesPopulation[a.BoatSide][Human]+hh;
b.SidesPopulation[a.BoatSide][Vampire]:=a.SidesPopulation[a.BoatSide][Vampire]-vv;
b.SidesPopulation[not a.BoatSide][Vampire]:=a.SidesPopulation[a.BoatSide][Vampire]+vv;
ok:=Transport(b);
end;
if ok then WriteCondition(a);
Result:=ok;
end;
end.