Recursive Back-to-Back Algorithm Application-eight queens

Source: Internet
Author: User

Once upon a time, there was an emperor who had eight queens, which resulted in a series of messy problems. Haha

Joke ~~~~


The eight queens issue is an ancient and famous issue and is a typical case of backtracking algorithms. This problem was raised by the international chess player Max besell in 1848: 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. After the computer is invented, there are multiple ways to solve this problem.


Specific algorithms:

# Include "stdafx. h" # include
 
  
# Define N 8 typedef struct _ tag_Pos {int ios; int jos;} Pos; static char board [N + 2] [N + 2]; static Pos [] ={{-1,-1 },{-1, 0 },{-1, 1 }}; static int count = 0; void init () {int I = 0; int j = 0; for (I = 0; I
  
   
N) {count ++; printf ("Solution: % d \ n", count); display (); getchar () ;}else {for (j = 1; j <= N; j ++) {if (check (I, j) {board [I] [j] = '*'; find (I + 1 ); board [I] [j] = '';}}} int main () {init (); find (1); system (" pause "); return 0 ;}
  
 

Result: a total of 92 solutions are provided for the eight queens, which are not listed here.

For more information, see your running results ~~~~~





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.