20150410 Recursive implementation Eight Queens question

Source: Internet
Author: User

20150410 Recursive implementation Eight Queens question

2015-04-10 Lover Snow Child

19th century the famous mathematician Gauss 1850 presented:

Put eight queens on the 8x8 chess, so that they can not attack each other, that arbitrary even a queen can not be in the same row, the same column or a unified slash, ask how many kinds of pendulum.

Here is one of the solutions:

Mr. Gauss's calculation of the night and night, concluded that 76 kinds.

In fact the correct conclusion is 92, here we will be programmed to calculate the correct answer.

1 //Eight Queen's question2#include <stdio.h>3 4 Static intCount =0;//total number of algorithms5 6 //determine if row col position is dangerous, whether in the other Queen's attack range7 //no danger, then return True8 intNotdanger (intRowintColint(*chess) [8]){9     intI, K;Ten     intflag_1=0, flag_2=0, flag_3=0, flag_4=0, flag_5=0; One     //judging the direction of the column there is no danger sign bit flag_1 A      for(i=0; i<8; i++) -     { -         if(* (* (chess + i) + col)! =0)//determine if there are any pieces the         { -Flag_1 =1; -              Break; -         }     +     } -     //judging the upper left there is no danger sign bit flag_2 +      for(i = row, k = col; i>=0&& k>=0; I--, k--) A     { at         if(* (* (chess + i) +k)! =0) -         { -Flag_2 =1; -              Break; -         } -     } in     //judging the lower right there is no danger sign bit flag_3 -      for(i = row, k = col; i<8&& k<8; i++, k++) to     { +         if(* (* (chess + i) +k)! =0) -         { theFlag_3 =1; *              Break; $         }Panax Notoginseng     } -     //judging the upper right there is no danger sign bit flag_4 the      for(i = row, k = col; i>=0&& k<8; I--, k++) +     { A         if(* (* (chess + i) +k)! =0) the         { +Flag_4 =1; -              Break; $         } $     } -     //Judging the lower left there is no danger sign bit flag_5 -      for(i = row, k = col; i<8&& k>=0; i++, k--) the     { -         if(* (* (chess + i) +k)! =0)Wuyi         { theFlag_5 =1; -              Break; Wu         } -     } About     if(flag_1 | | flag_2 | | flag_3 | | flag_4 | |flag_5) { $         return 0; -}Else{ -         return 1; -     } A } +  the //Eight Queen algorithm row: Start Row col: number of columns *chess[8]: pointer to each row of the board - voidEightqueen (intRowintColint(*chess) [8]) $ { the     intchess_tmp[8][8], I, J; the      for(i =0; i<8; i++){ the          for(j =0; j<8; J + +){ theCHESS_TMP[I][J] =Chess[i][j]; -         } in     } the     //when the pointer has reached the eighth line, the instructions have been calculated to print out the board the     if(8==row) { Aboutprintf"method%d: \ n", count+1); the          for(i =0; i<8; i++){ the              for(j =0; j<8; J + +){ theprintf"%d", * (* (chess_tmp+i) +j)); +             } -printf"\ n"); the         }    Bayiprintf"\ n"); thecount++; the}Else{ -         //Judging if this position is dangerous, whether the other Queen's attack range -         //If there is no danger, continue to judge, know row = 8 the          for(j=0; j<col; J + +){ the             if(Notdanger (Row, J, Chess_tmp)) {//Judging whether it is dangerous the                  for(i =0; i<8; i++) {//assign a value of 0 to all columns of the entire row the* (* (chess_tmp + row) +i) =0; -                 } the* (* (chess_tmp + row) +j) =1; theEightqueen (row+1, col, chess_tmp);//Recursive invocation the             }94         } the     } the } the 98 intMainvoid) About { -     intchess[8][8], I, J;101 102     //Initialize eight Queen's chessboard, 1: Place Queen 0: no queen103      for(i =0; i<8; i++){104          for(j =0; j<8; J + +){ theCHESS[I][J] =0;106         }107     }108     109Freopen ("EightQueen.txt","W", stdout);//REDIRECT output to the OUT.txt file the 111Eightqueen (0,8, chess); theprintf"There are a total of%d solutions. \ n", count);113  the     return 0; the}

After the successful run, generate a file EightQueen.txt in the current folder, as shown in the following:

20150410 Recursive implementation Eight Queens question

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.