Implementation of "Eight Queens" Dynamic Graphics

Source: Internet
Author: User

The eight queens issue is an ancient and famous issue and is a typical example of backtracking algorithms. This problem was raised by the famous mathematician Gauss in the 19th century in 1850: Eight queens were placed on 8x8 gags of chess, so that they could not attack each other, that is to say, neither of the two queens can be in the same row, column, or diagonal line.
Gaussian believes there are 76 solutions. In 1854, different authors published 40 different solutions in the Berlin chess magazine. Later, they used Graph Theory to solve 92 solutions.
For the implementation of the eight queens problem, if combined with dynamic graphics demonstration, it can make the description of the algorithm more vivid, so that the teaching can produce good results. The following is a graphic program for the eight queens Problem implemented by the author using Turbo C, which can demonstrate all 92 groups of solutions. The following two problems should be solved to achieve the dynamic graphics of the eight queens problem.

1. Implementation of backtracking Algorithms
(1) To solve this problem, we set the horizontal coordinates of the Board as I, the vertical coordinates as j, and the values of I and j are from 1 to 8. When a queen occupies the position (I, j), no other queen can be placed in the vertical direction, horizontal direction, or diagonal line. The statement can be used to define the following three integer Arrays: a [8], B [15], and c [24]. Where:

A [J-1] = 1 column j no queen
A [J-1] = 0 column j has Queen
B [I + J-2] = 1 (I, j) diagonal (top left to bottom right) No queen
B [I + J-2] = 0 (I, j) diagonal (top left to bottom right) Has queen
C [I-j + 7] = 1 (I, j) diagonal (top right to bottom left) No queen
C [I-j + 7] = 0 (I, j) the diagonal line (top right to bottom left) has a queen

(2) The algorithm for selecting the position of Queen I is as follows:

For (j = 1; j <= 8; j ++)/* Queen I in row j */
If (I, j) is null)/* indicates that the corresponding element value of the three arrays is 1 */
{Occupied position (I, j)/* sets the element value corresponding to the three arrays to 0 */
If I <8
Select a proper position for I + 1 queen;
Else outputs a solution
}

2. Graphic access
In the Turbo C language, the following standard functions can be used to access images:

Size = imagesize (x1, y1, x2, y2); return the number of bytes required for the storage area.
Arrow = malloc (size); create a dynamic region bitmap of the specified size, and set a pointer to arrow.
Getimage (x1, y1, x2, y2, arrow); stores the bitmap of the specified region in a buffer zone.
Putimage (x, y, arrow, copy) places the bitmap in the upper left corner of the screen (x, y.

3. The program list is as follows:

# Include <graphics. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <dos. h>
Char n [3] = {0, 0};/* used to record the group of solutions */
Int a [8], B [15], c [24], I;
Int h [8] = {127,177,227,277,327,377,427,477};/* the line coordinate of each queen */
Int l [8] = {252,217,182,147,112, 7};/* column coordinates of each queen */
Void * arrow;
Void try (int I)
{Int j;
For (j = 1; j <= 8; j ++)
If (a [J-1] + B [I + J-2] + c [I-j + 7] = 3)/* if column I j behavior is empty */
{A [J-1] = 0; B [I + J-2] = 0; c [I-j + 7] = 0;/* occupies column I j */
Putimage (h [I-1], l [J-1], arrow, COPY_PUT);/* display Queen graphics */
Delay (500);/* latency */
If (I <8) try (I + 1 );
Else/* output a group of solutions */
{N [1] ++; if (n [1]> 9) {n [0] ++; n [1] = 0 ;}
Bar (260,300,390,340);/* displays the solution of group n */
Outtextxy( 275,300, n );
Delay (3000 );
}
A [J-1] = 1; B [I + J-2] = 1; c [I-j + 7] = 1;
Putimage (h [I-1], l [J-1], arrow, XOR_PUT);/* remove the queen, continue searching for the next group of solutions */
Delay (500 );
}
}
Int main (void)
{Int gdrive = DETECT, gmode, errorcode;
Unsigned int size;
Initgraph (& gdrive, & gmode ,"");
Errorcode = graphresult ();
If (errorcode! = GrOk)
{Printf ("Graphics error"); exit (1 );}
Rectangle );
Rectangle (60, 25, 90, 33 );
/* Draw the Crown */
Line (60, 28, 90, 28); line (60, 25, 55, 15 );
Line (55,15, 68,25); line (68,25, 68,10 );
Line (,); line );
Line (82,10, 82,25); line (82,25, 95,15 );
Line );
Size = imagesize (,); arrow = malloc (size );
Getimage (, 38, arrow);/* Save the crown to the buffer */
Clearviewport ();
Settextstyle (TRIPLEX_FONT, HORIZ_DIR, 4 );
Setusercharsize (3, 1, 1, 1 );
Setfillstyle (1, 4 );
For (I = 0; I <= 7; I ++) a [I] = 1;
For (I = 0; I <= 14; I ++) B [I] = 1;
For (I = 0; I <= 23; I ++) c [I] = 1;
For (I = 0; I <= 8; I ++) line (125, I * 35 + 5,525, I * 35 + 5);/* draw a Board */
For (I = 0; I <= 8; I ++) line (125 + I * 50, 5, 125 + I * 50,285 );
Try (1);/* Call recursive functions */
Delay (3000 );
Closegraph ();
Free (arrow );
}

Related Article

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.