For the implementation of the eight Queens problem, if combined with the dynamic graphical demonstration, it can make the description of the algorithm more vivid, and make the teaching can produce good effect.
Key words Eight queens problem conflict data structure thread class
The eight Queens problem is an old and famous problem, it is a typical example of backtracking algorithm. The problem is the 19th century famous mathematician Gauss 1850 proposed: In 8x8 chess, put eight queens, so that they can not attack each other, that is, any two queens can not be in the same row, the same column or the same slash, ask how many pendulum method.
The following is a dynamic graphics program for the eight Queens problem implemented by DELPHI6, which can demonstrate all 92 groups of solutions. Eight Queens Problem the realization of dynamic graph, mainly should solve the following several problems.
Conflict
Include rows, columns, and two diagonal lines:
(1) Column: The provision of a queen for each column will not cause conflict on the list;
(2) Line: When the first line is occupied by a queen, then all the spaces on the same line can no longer be placed on the Queen, to take I
For the subscript marks are placed for the occupied state;
(3) Diagonal: The diagonal has two directions. All points on the same diagonal line (set subscript (i,j)), either (I+J) are constants, or (I-J) are constants. Therefore, when the first Queen seizes the column J, the mark of (I+j), (i-j) for subscript is placed in the occupied state at the same time.
Data
In order to control the execution of the problem, the main data in the problem and the corresponding operation should be defined as a thread class. Method: Click the other option in the new menu, select Thread object in the dialog box, and the class name of the thread class in Classs name. The specific definition is as follows:
type
Tbhh = class(TThread)
private
a:array[1..8,1..8]of integer;
tt:integer;
q,c:Tbitmap;
procedure prt;
function pd(i,j:integer):boolean;
procedure hsu(i:integer);
protected
procedure Execute; override;
public
constructor create(flag:boolean);
end;
var
dstep:boolean;