program damy; { Hleda vsechna rozmisteni M dam na sachovnici M x M } { v nichz se damy navzajem neohrozuji } const M = 8; { rozmer sachonice } U1 = 1-M; U2 = M-1; D1 = 2; D2 = M+M; var X : array[1..M] of 0..M; { X[J] je cislo sloupce, na nemz stoji dama v J-te rade } { pokud tam neni - je hodnota X[J] nula } COL : array[1..M] of boolean; { COL[I] - I-ty sloupec je volny } UP : array[U1..U2] of boolean; { (I-J)-ta rostouci diagonala je volna } DOWN : array[D1..D2] of boolean; { (I+J)-ta klesajici diagonala je volna } procedure inic; {inicializuje prazdnou sachovnici } var I:integer; begin for I:=1 to M do begin X[I] := 0; {v I-te rade neni dama} COL[I] := true; {sloupec I je volny} end; {vsechny diagonaly volne:} for I:=U1 to U2 do UP[I] := true; for I:=D1 to D2 do DOWN[I] := true; end; procedure tisk; var I:integer; begin for I:=1 to M do write(X[I]:4); writeln; end; procedure Gen(J:integer); var I :integer; begin for I:=1 to M do if COL[I] and UP[I-J] and DOWN[I+J] then begin {pole (J,I) je volne } { polozeni damy na pole (J,I) } X[J] := I; COL[I] := false; UP[I-J] := false; DOWN[I+J]:= false; if J=M then tisk { uz je polozeni vsech M dam } else Gen(J+1); { zkusim polozit dalsi damu } { zvednuti damy z pole (J,I) } X[J] := 0; COL[I] := true; UP[I-J] := true; DOWN[I+J]:= true; end; end {of proc Gen}; begin inic; gen(1); end.