Take a closer look at the Baidu in the backtracking method introduced, This is a very useful algorithm, there are probably two modes, one is traversal, one is Recursion.
I put both of these methods listed, according to the internet, recursion efficiency is much faster than traversal, I test here is the same, perhaps the network of those traversal method is not optimized well,
I've been through a lot of things.
There is no Delphi of the original code, I synthesized a variety of algorithms, the N-order Queen's algorithm is written Out. The following is the original code, I hope that interested friends to study with me message:
Project File: queen8.dpr, The following code is compiled under Delphi2010.
Program Queen8;
Uses
Forms,
UQueen8 in ' Uqueen8.pas ' {fmQueen8};
{$R *.res}
Begin
application.initialize;
Application.mainformontaskbar: = True;
Application.createform (TfmQueen8, fmQueen8);
application.run;
End.
Form cell Files: Uqueen8.pas
Unit uQueen8;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics,
Controls, Forms, Dialogs, stdctrls, Buttons,
Spin, extctrls, strutils,dateutils;
Type
TfmQueen8 = Class (tform)
spn1:tspinedit;
btnrecurrence:tbitbtn;
mem1:tmemo;
lbl1:tlabel;
pnl1:tpanel;
mem2:tmemo;
btngothrough:tbitbtn;
Procedure Btnrecurrenceclick (sender:tobject);
Procedure Btngothroughclick (sender:tobject);
Private
{Private declarations}
Queen:array of Integer;
stylecount:integer;
Public
{public declarations}
Procedure Outputstyle (astrs:tstrings; Aqueen:array of Integer);
End
Var
fmqueen8:tfmqueen8;
Implementation
{$R *.dfm}
The
{input and judgment should be two common methods, both traditional traversal and recursion are the Same. }
Procedure Outputdata (astrs:tstrings; Aqueen:array of Integer; astylecount:integer);
Var
asize:integer;//array length
aline:string; //record line
i, j:integer;//cyclic variable
be Gin
//astrs.clear;
astrs.append (' ==styleno: ' +inttostr (astylecount) + ' = = ');
asize: = high (aqueen)-low (aqueen) +1;
For I: = Low (aqueen) to high (aqueen) do
begin
ALine: = dupestring (' ☆ ', aqueen[i ]) + ' ★ ' + dupestring (' ☆ ', asize-aqueen[i]-1);
astrs.append (ALine);
end;
Astrs.append (' ========== ');
end;
Determine if the position of the new Queen is established, its position coordinates x, y
function Judgequeen (aqueen:array of Integer; apositiony, apositionx:integer): Boolean;
Var
x, y:integer; x, y corresponds to the horizontal and vertical axis of its temporary comparison value
Begin
Result: = False;
If Apositiony>high (aqueen) Then
Exit;
For y: = 0 to APositionY-1 do
Begin
x: = aqueen[y];
If X=apositionx then//compare vertical lines
Exit;
If X+y=apositionx+apositiony Then//compare left top to bottom right slash
Exit;
If X-y=apositionx-apositiony then//compare right top to bottom left slash
Exit;
End
Result: = True;
End
{TfmQueen8}
Procedure Tfmqueen8.btnrecurrenceclick (sender:tobject);
Var
i,stylecount:integer;
Queen:array of Integer; Queens Data
queensize:integer; Number of Queens
timetemp:tdatetime;
Perform the Queen's Recursive calculation
Procedure Calc (aqueen:array of Integer; aindex:integer; var astylecount:integer); Aindex meaning is, has successfully examined several orders
Var
i:integer;
Begin
For I: = Low (aqueen) to high (aqueen) does
Begin
If Judgequeen (aqueen,aindex,i) Then
Begin
Aqueen[aindex]: = i;
If Aindex=high (aqueen)-low (aqueen) Then
Begin
INC (astylecount);
Outputdata (mem1. lines,aqueen,astylecount);
Aqueen[aindex]: = 0;
Sleep (1000);
break;
End
Else
Calc (aqueen,aindex+1,stylecount);
Aqueen[aindex]: =i;
End
End
End
Begin
Stylecount: = 0;
Queensize: =spn1. Value;
SetLength (queen,queensize);
For I: = Low (Queen) to high (Queen) does
Begin
Queen[i]: = 0;
End
Timetemp: = now;
Calc (queen,0,stylecount);
mem1. Lines.append (#13 + ' total time spent ' +inttostr (millisecondsbetween (timetemp,now) + ' msec ');
End
Procedure Tfmqueen8.btngothroughclick (sender:tobject);
Var
i,stylecount:integer;
Queen:array of Integer; Queens Data
queensize:integer; Number of Queens
timetemp:tdatetime;
Perform a Queen's Traversal calculation
Procedure Calc (aqueen:array of Integer; var astylecount:integer);
Var
i, index:integer; Index meaning is the order of the current change
Begin
Index: = 0; Starting with the first number, this is the same as the recursive initialization parameter.
While the index>=0 do//loops are special, it is not possible to use a For loop because the loop control is Complex.
Begin
INC (aqueen[index]); The value is assigned, and then the assigned data is Judged.
While (aqueen[index]<=high (aqueen)-low (AQUEEN)) and not (judgequeen (aqueen,index,aqueen[index))) do
INC (aqueen[index]); When the current data check does not pass, go directly to the next column of the current ROW.
If (aqueen[index]<=high (aqueen)-low (aqueen)) and (index=high (aqueen)-low (AQUEEN)) Then
Begin//when The data Check passes, and index is full-scale, it is output directly.
INC (astylecount);
Outputdata (mem2. lines,aqueen,astylecount);
End
else if (aqueen[index]<=high (aqueen)-low (aqueen)) and (indexBegin
INC (Index); When you are dissatisfied with the order, go directly to the next Line.
End
Else//last such case, actually aqueen[index] has gone beyond the boundary,
Begin//this Line does not have a suitable position at all, then jump on a line, and let the previous row increase
aqueen[index]:=-1;
Dec (Index); Here you only need to adjust the row, column adjustment in the next loop first sentence processing.
End
End
End
Begin
Stylecount: = 0;
Queensize: =spn1. Value;
SetLength (queen,queensize);
For I: = Low (Queen) to high (Queen) does
Begin
Queen[i]: =-1;
End
Timetemp: = now;
Calc (queen,stylecount);
mem2. Lines.append (#13 + ' total time spent ' +inttostr (millisecondsbetween (timetemp,now) + ' msec ');
End
Procedure Tfmqueen8.outputstyle (astrs:tstrings; Aqueen:array of Integer);
Var
qsize:integer;
i,j:integer;
aline:string;
Begin
qsize: = High (aqueen)-low (aqueen);
unfinished, Here you want to define the various symbolic styles of the Output.
End
End.
Form code file UQUEEN8.DFM
Object Fmqueen8:tfmqueen8
left = 0
Top = 0
Caption = ' Queen8 '
ClientHeight = 388
ClientWidth = 528
Color = Clbtnface
Font.charset = Default_charset
Font.Color = Clwindowtext
Font.height =-11
Font.Name = ' Tahoma '
Font.style = []
Oldcreateorder = False
PixelsPerInch = 96
TextHeight = 13
Object Lbl1:tlabel
Left = 8
Top = 8
Width = 48
Height = 13
Caption = #30343 #21518#25968#30446
End
Object Spn1:tspinedit
Left = 62
Top = 6
Width = 121
Height = 22
MaxValue = 0
MinValue = 0
TabOrder = 0
Value = 8
End
Object Btnrecurrence:tbitbtn
left = 32
Top = 44
Width = 137
Height = 25
Caption = #39640 #25928#36882#24402#22238#26388#27861#35745#31639
doublebuffered = True
parentdoublebuffered = False
TabOrder = 1
OnClick = Btnrecurrenceclick
End
Object Pnl1:tpanel
left = 0
Top = 88
Width = 528
Height = 300
Align = Albottom
TabOrder = 2
Object Mem1:tmemo
left = 1
Top = 1
Width = 255
Height = 298
Align = Alclient
TabOrder = 0
End
Object Mem2:tmemo
left = 256
Top = 1
Width = 271
Height = 298
Align = Alright
TabOrder = 1
End
End
Object Btngothrough:tbitbtn
Left = 280
Top = 44
Width = 169
Height = 25
Caption = #20256 #32479#36941#21382#22238#26388#27861#35745#31639
doublebuffered = True
parentdoublebuffered = False
TabOrder = 3
OnClick = Btngothroughclick
End
End
The study of the eight Queen's Backtracking calculation method