The dynamic realization of the eight Queens problem based on Delphi

Source: Internet
Author: User
Tags thread thread class

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.