if CountForCorrect <> QueensCount * QueensCount then
buttonSelected := MessageDlg('Unfortunately, you did not place all the queens with hints. Do you want to try again?', mtInformation, [mbYes, mbNo], 0);
if buttonSelected = mrYes then
//...
procedure RecursiveSolution(RestoreLastSolution: boolean = false);
var
i: Integer;
a: array [1 .. MaxQueensCount] of Boolean; // a[j] - на j-й горизонтали ферзя нет
b: array [2 .. 2 * MaxQueensCount] of Boolean; // b[k] - на k-й / диагонали ферзя нет (у всех полей на диагонали / постоянна сумма координат i и j)
c: array [1 - MaxQueensCount .. MaxQueensCount - 1] of Boolean; // c[k] - на k-й \ диагонали ферзя нет (у всех полей на диагонали \ постоянна разность координат i и j)
x: array [1 .. MaxQueensCount] of Integer; // x[i] - местоположение ферзя на i-й вертикали
procedure Solve(i: Integer);
var
j: Integer;
begin
if RestoreLastSolution then
Begin
if I <> QueensCount then
Solve(I + 1)
else
Begin
RestoreLastSolution := False;
Exit;
End;
End;
if not RestoreLastSolution then
Begin
if not StopPressed then
for j := 1 to QueensCount do
begin
if a[j] and b[i + j] and c[i - j] then
begin
x[i] := j;
a[j] := False;
b[i + j] := False;
c[i - j] := False;
if i < QueensCount then
Begin
Cout1 := j;
Cout2 := i;
Solve(i + 1)
End
else
frmMain.DrawSolution(x);
a[j] := True;
b[i + j] := True;
c[i - j] := True;
end;
end;
End;
end;
if RestoreLastSolution then
Begin
if I <> QueensCount then
Solve(i + 1)
else
Begin
RestoreLastSolution := False;
Exit;
End;
End;
procedure RecursiveSolution(RestoreLastSolution: boolean = false);
var
i: Integer;
a: array [1 .. MaxQueensCount] of Boolean; // a[j] - на j-й горизонтали ферзя нет
b: array [2 .. 2 * MaxQueensCount] of Boolean; // b[k] - на k-й / диагонали ферзя нет (у всех полей на диагонали / постоянна сумма координат i и j)
c: array [1 - MaxQueensCount .. MaxQueensCount - 1] of Boolean; // c[k] - на k-й \ диагонали ферзя нет (у всех полей на диагонали \ постоянна разность координат i и j)
x: array [1 .. MaxQueensCount] of Integer; // x[i] - местоположение ферзя на i-й вертикали
procedure Solve(i: Integer);
var
j: Integer;
begin
if RestoreLastSolution then
Begin
if I <> QueensCount then
Solve(i + 1)
else
Begin
RestoreLastSolution := False;
Exit;
End;
End;
if not RestoreLastSolution then
Begin
if not StopPressed then
for j := 1 to QueensCount do
begin
if a[j] and b[i + j] and c[i - j] then
begin
x[i] := j;
a[j] := False;
b[i + j] := False;
c[i - j] := False;
if i < QueensCount then
Begin
Cout1 := j;
Cout2 := i;
Solve(i + 1)
End
else
frmMain.DrawSolution(x);
a[j] := True;
b[i + j] := True;
c[i - j] := True;
end;
end;
End;
end;
begin
if not RestoreLastSolution then
Begin
for i := 1 to QueensCount do
a[i] := True;
for i := 2 to 2 * QueensCount do
b[i] := True;
for i := 1 - QueensCount to QueensCount - 1 do
c[i] := True;
End;
if RestoreLastSolution then
begin
For i := 1 to QueensCount do
x[i] := TmpArr[i];
a[Cout1] := True;
b[Cout2 + Cout1] := True;
c[Cout2 - Cout1] := True;
i:=Cout2;
Solve(i);
end;
Solve(1);
end;
procedure RecursiveSolution(RestoreLastSolution: boolean = false);
var
i: Integer;
a: array [1 .. MaxQueensCount] of Boolean; // a[j] - на j-й горизонтали ферзя нет
b: array [2 .. 2 * MaxQueensCount] of Boolean; // b[k] - на k-й / диагонали ферзя нет (у всех полей на диагонали / постоянна сумма координат i и j)
c: array [1 - MaxQueensCount .. MaxQueensCount - 1] of Boolean; // c[k] - на k-й \ диагонали ферзя нет (у всех полей на диагонали \ постоянна разность координат i и j)
x: array [1 .. MaxQueensCount] of Integer; // x[i] - местоположение ферзя на i-й вертикали
procedure Solve(i: Integer);
var
j: Integer;
begin
if not StopPressed then
for j := 1 to QueensCount do
begin
if a[j] and b[i + j] and c[i - j] then
begin
x[i] := j;
a[j] := False;
b[i + j] := False;
c[i - j] := False;
if i < QueensCount then
Begin
Cout1 := j;
Cout2 := i;
Solve(i + 1)
End
else
frmMain.DrawSolution(x);
a[j] := True;
b[i + j] := True;
c[i - j] := True;
end;
end;
end;
procedure Solve1(i: Integer);
var
j: Integer;
begin
if i = QueensCount then
Exit;
for j := 1 to QueensCount do
begin
if a[j] and b[i + j] and c[i - j] then
begin
x[i] := j;
a[j] := False;
b[i + j] := False;
c[i - j] := False;
if i < QueensCount then
Solve1(i + 1)
else
frmMain.DrawSolution(x);
a[j] := True;
b[i + j] := True;
c[i - j] := True;
end;
end;
end;
begin
if not RestoreLastSolution then
Begin
for i := 1 to QueensCount do
a[i] := True;
for i := 2 to 2 * QueensCount do
b[i] := True;
for i := 1 - QueensCount to QueensCount - 1 do
c[i] := True;
End;
if RestoreLastSolution then
begin
For i := 1 to QueensCount do
x[i] := TmpArr[i];
a[Cout1] := True;
b[Cout2 + Cout1] := True;
c[Cout2 - Cout1] := True;
Solve1(1);
end;
Solve(1);
end;