A retrospective approach to the four Queens problem

Source: Internet
Author: User

Take the 4 Queen as an example, the other n Queens question and so on. The so-called 4 queen problem is solving how to put 4 queen pieces on a 4x4 chessboard without conflict. In chess, the Queen moves in a horizontal and vertical way, so that no Queen's pieces can be seen at the level, upright, or 45-degree slash of any queen's position, for example

Requires programming to find out the number of cases that meet the requirements. There are a number of solutions to the four Queens problem, here is a classic solution: The backtracking method

The basic idea of backtracking is that you can construct a solution space tree, and by exploring the spatial tree of the solution, you can get one or several solutions to the problem of four queens. There are four trees in this space.

In the 4x4 board as shown, by column to lay pieces, first of all, because the Queen can not be in the same column, so first ruled out there are 2 or more than 2 pieces in the same column, so the first piece in the first column there are 4 placement methods (1th column 1th row, 1th column 2nd row, 1th column 3rd row, 1th column 4th line) The second piece in the second column there are 4, the same third piece in the third column there are 4, the same fourth pieces in the fourth column there are 4, so the simple exclusion of the case is not the same column, there are 4x4x4x4=256, but in these 256 possible, still exist such as chess pieces on the same line, Or on a 45-degree slash. Another way of thinking, all the four queens to meet the problem of placement must exist in these 256 cases. The simple understanding is that these 256 checkerboard situations contain all the solutions that satisfy the 4 queen problem, but do not include all the checkerboard situations.

The following is an example of a spatial tree (a column-by-row approach for example), where the checkerboard situation in layer I is evolving from the checkerboard situation on the i-1 level (1<I<4)

The above picture is a solution space tree derived from the first row of the first piece in the first column, the last layer will have 64 knots, the same as the first piece in the first, the column of the second/three/four row can be derived from a solution space tree, the last layer will have 64 in the situation, so there are 4 solution space tree, Each tree eventually has 64 situations, so there is a total of 4x64=256 situation

You can use the above method to enumerate all the solutions, and then traverse all the results of the exhaustive to find all the solutions that meet the four Queens problem, but this will be very wasteful. So here you can use backtracking, in the process of building a solution to the spatial tree in depth first exploration, when the exploration of a certain chessboard situation must not be the solution of the four Queens problem (such as the occurrence of any two or more than two pieces in the same row/the same column/45-degree slash), It can be judged that the node of the solution space tree derived from this node is not the solution of the four Queens problem, so it can avoid a lot of useless work.

For example, in the second row of the first node in the case of two pieces in the same row, so it can be judged that the node and the node is derived from all the nodes are no longer necessary to traverse, which will avoid the 4+4x4 of the completely useless traversal, will greatly save time, Then explore the second node of the second row ... The other same.

In this way, if you can successfully traverse to the leaf node, and judge the situation of the leaf node is in line with the 4 queen problem, then this node situation represents a legitimate four Queen problem solution. The image below represents the process of finding a legal solution (note that the line in the picture, the dashed lines are excluded, the black solid lines represent the continuation of the downward exploration) For example, when the illegal checkerboard situation on layer I, jump back to the first layer of i-1, continue to explore the next branch of the node i-1 layer Or, on the 4th level, explore the legal situation and record it and jump back to the previous level to continue exploring the next branch. The other three solvable spatial trees are the same.

For example, just look at the number of fourth-tier nodes explored. With backtracking, you only need to explore the 4 nodes in the 4th layer, and if you use the exhaustive method, you will have to explore all 64 nodes in the 4th layer, which is obviously more effective.

In fact, in solving the problem of the four queens, do not have to really build a solution to the space tree, it can be fully simulated by a recursive backtracking. The so-called solution space tree is just a logical abstraction. Of course, the tree structure can be used to create a real solution to the space tree, but that would be a waste of space resources, there is no need to

The algorithm for solving the four Queens problem is described below

1#include <stdio.h>2 3 intCount =0;4 intIscorrect (intIintJint(*Q) [4])5 {6     ints, t;7      for(s=i,t=0; t<4; t++)8         if(q[s][t]==1&& t!=j)9             return 0;//Judging RowsTen      for(t=j,s=0; s<4; s++) One         if(q[s][t]==1&& s!=i) A             return 0;//Judging Columns -      for(s=i-1, t=j-1; s>=0&&t>=0; S--, t--) -         if(q[s][t]==1) the             return 0;//Judging top left -      for(s=i+1, t=j+1; s<4&&t<4; s++,t++) -         if(q[s][t]==1) -             return 0;//Judging bottom Right +      for(s=i-1, t=j+1; s>=0&&t<4; S--, t++) -         if(q[s][t]==1) +             return 0;//Judging top Right A      for(s=i+1, t=j-1; s<4&&t>=0; s++,t--) at         if(q[s][t]==1) -             return 0;//Judging Bottom left -  -     return 1;//otherwise return - } -  in voidQueue (intJint(*Q) [4]) - { to     inti,k; +     if(j==4){//Recursive End Condition -          for(i=0; i<4; i++){ the                 //get a solution that is displayed on the screen *              for(k=0; k<4; k++) $printf"%d", Q[i][k]);Panax Notoginsengprintf"\ n"); -         } theprintf"\ n"); +count++; A         return ; the     } +      for(i=0; i<4; i++){ -         if(Iscorrect (I, J, Q)) {//if Q[I][J] can place queen $q[i][j]=1;//Place Queen $Queue (j+1, Q);//Recursive Depth First search solution space tree -q[i][j]=0;//This code is the implementation back to the previous level -         } the     } - }Wuyi  the intMain () - { Wu     intq[4][4]; -     intI, J; About      for(i=0; i<4; i++) $          for(j=0; j<4; J + +) -Q[I][J] =0; -Queue (0, Q); -printf"The number of the answers is%d\n", count); A     return 0; +}

A retrospective approach to the four Queens problem

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.