Eight queens:
The eight Queens question, an old and famous problem, is a typical case of backtracking algorithms. The problem is the international chess player Max Bessel in 1848: Put eight queens on the 8x8 chess, 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 kinds of pendulum. Gauss thinks there are 76 kinds of schemes. In 1854, in the Chess magazine in Berlin, different authors published 40 different solutions, and later some of them solved 92 kinds of results using the method of graph theory. After the invention of computer, there are many computer languages to solve this problem.
Icon:
My solution:
There are a lot of methods on the Internet, most of the abstraction is difficult to understand, and the great God has sorted out the version of 10 lines of code, a time of shock for heaven and the people were startled. How to write eight queens in C + + in 10 rows? You can close this article if you understand the force of the great Gods of each road.
Now give my personal version, the algorithm process description of the more detailed, and added a lot of comments, especially the process control of the annotations more, easy to understand. The core of the algorithm is based on the location of the unknown location calculation. This implementation is not the most efficient way, but it is very helpful for those who have just contacted this algorithm, and have not collated their ideas.
Results
The source code is as follows : Eight Queens
Another implementation of the Eight Queens algorithm (C # version)