home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1997 April
/
Chip_1997-04_cd.bin
/
tema
/
602propc
/
disk12
/
data.12
/
WINTEXT
/
MAKRA
/
SOURCES
/
DAMY.TXT
< prev
next >
Wrap
Text File
|
1995-02-13
|
4KB
|
151 lines
Program Damy;
// makro heuristick²m algoritmem °eÜφcφ obtφ₧n∞jÜφ
// kombinatorickou ·lohu
const
MINP = 2;
MAXP = 9;
DEFAULTP = 8;
var
positions : array[1..MAXP] of integer;
rozmer : integer;
nalezlo : integer;
user_stop : boolean;
S : string[30];
function zadej_mez(var mez : integer) : boolean;
var
result, ok : boolean;
s1, s2 : string[10];
sCapt: string[30];
tmp : integer;
begin
s1 := Int2Str(MINP);
s2 := Int2Str(MAXP);
sCapt:="Rozm∞r Üachovnice (" + s1 + " - " + s2 + " ):";
s1:= Int2Str(DEFAULTP);
repeat
result := Input_Box(sCapt, s1, 2);
if result then
begin
tmp:=Str2Int(s1);
if tmp = NONEINTEGER then
begin
Info_box("Informace", "Chybn² zßpis celΘho Φφsla !");
ok:=false;
end
else if (tmp < MINP) or (tmp > MAXP) then
begin
Info_box("Informace", "Φφslo nenφ v dan²ch mezφch !");
ok:=false;
end
else ok:=true;
end
else ok:=true;
until not (result and not ok);
if result then mez := tmp;
zadej_mez:=result;
end;
function Message_stop : boolean;
var
i : integer;
sp : string[80];
sv : string[30];
begin
nalezlo:=nalezlo + 1;
sv:="";
for i:=1 to rozmer do
begin
sv:=sv + Char2Str(chr(ord('A') + i - 1));
sv:=sv + Int2Str(positions[i]) + " ";
end;
sp:= "Pozice Φ. " + Int2Str(nalezlo) + " :"+ #13#10 + sv;
sp:=sp + #13#10"PokraΦovat ?";
user_stop:= not YesNo_box("Informace", sp);
Message_stop:= user_stop;
end;
{ vracφ 0 nebyla-li nalezena novß pozice : }
function NewPos(sloupec, pos : integer) : integer;
var
result, i, r : integer;
ok : boolean;
begin
result := 0;
pos :=pos + 1;
{ projφt zb²vajici mo₧nΘ pozice ve sloupci : }
while (result = 0) and (pos <= rozmer) do
begin
ok := true; { otestovat novou mo₧nou pozici : }
i :=1;
while ok and (i < sloupec) do { projφt u₧ postavenΘ dßmy }
begin
r:=positions[i];
if (r = pos) or ((sloupec - i) = Iabs(pos - r))
then ok := false { ohro₧ujφ se }
else i:= i + 1; { otestuj dalÜφ dßmu }
end;
if ok then result := pos { novß pozice vyhovuje }
else pos:=pos + 1 { zkus dalÜφ pozici }
end;
NewPos := result;
end;
procedure Nalezni;
var
current, tmp : integer;
begin
nalezlo:= 0;
current := 1; { inicializace zßsobnφku pozic }
positions[1]:= 0;
while current > 0 do { dokud je zßsobnφk neprßzdn² : }
begin
tmp:=NewPos(current, positions[current]);
if tmp > 0 then { nalezena mo₧nß pozice ve sloupci : }
begin
positions[current]:=tmp; { dosa∩ ji }
if current = rozmer { je to poslednφ sloupec ? }
then begin
if not Message_stop then
current := current - 1 { vra¥ se o sloupec zp∞t }
else current:= 0; { ukonΦi }
end
else begin
current:=current + 1; { vezmi dalÜφ sloupec }
positions[current]:=0;
end;
end
else current := current - 1; { vra¥ se o sloupec zp∞t }
end;
end;
begin
if YesNo_box("Nabφdka :",
" Wintext vßm poradφ,"
" kterak na Üachovnici N x N "#13#10
"rozmφstit N dam tak,"
" aby se ₧ßdnΘ dv∞ neohro₧ovaly."#13#10
"PokraΦovat?") then
if zadej_mez(rozmer) then
begin
user_stop:=false;
Nalezni;
if (nalezlo > 0)
then
if user_stop
then Info_Box("Informace",
" UkonΦil(a) jste b∞h programu")
else begin
S := "Pozice Φ. " + Int2Str(nalezlo) +
" byla poslednφ .";
Info_Box("Informace", S)
end
else
Info_Box("Politovßnφ",
"Pro tento rozm∞r Üachovnice "
"₧ßdnΘ vyhovujφcφ rozmφst∞ni neexistuje")
end;
end.