Question of the eight Queens

Source: Internet
Author: User

"C and Pointers" chapter 8th programming exercises 8th:

1 /*2 * * Eight Queen's question3 */4 5#include <stdio.h>6 #defineTRUE 17 #defineFALSE 08 9 /*Ten * * Chessboard, 8*8 's two-dimensional matrix, is a global variable One * * will have Queen's place set to True A * * FALSE, no queen - */ - intchessboard[8][8] = {0 }; the  - intConflictintRowintcol); - voidPrint_board (); - voidPlace_queen (introw); +  - intMain () + { APlace_queen (0 ); at     return 0; - } -  - /* - * * Function function: - * * Given a row, place the Queen on each column of the row to check for attacks against each other in */ - void  toPlace_queen (introw) + { -     intCol; the      *     /* $ * * Try to place the Queen on each column of the rowPanax Notoginseng     */ -      for(col =0; Col <8; ++Col) the     { +chessboard[row] [col] =TRUE; A          the         /* + * * Check if the Queen is in conflict with the other Queens - * * The 1th row of Queens is placed 1th, certainly not with other conflicts $ * * Lines 1th and 2nd to 7th just put on the Queen does not conflict with other when placing the next queen with recursion $ * * Line 8th put on the Queen does not conflict with other when printing the answer -         */ -         if(Row = =0|| !conflict (Row, col)) the         { -             if(Row <7 )WuyiPlace_queen (Row +1 ); the             Else - Print_board (); Wu         } -          About         //The current position is placed on the queen with other conflicts, reset the position to False $chessboard[row] [col] =FALSE; -     } - } -  A /* + * * Function function: the * * Check if the Queen is in conflict with the other queens at a certain place on the Board - * * Description: $ * * Because the Queen is placed on a line from the top down, only need to check the * * The Queen before the current position, the Queen has not been placed after the current position the The * * function returns: the * * conflict, return true; no conflict, return false the */ - int inConflictintRowintCol) the { the     inti; About      for(i =1; I <8; ++i) the     { the         //Check if there is a queen on the line above the current piece. the         if(Row-i >=0&& chessboard[Row-i] [col]) +             returnTRUE; -              the         //Check if there is a queen on the left line of the current pawnBayi         if(Col-i >=0&& chessboard[Row] [col-i]) the             returnTRUE; the              -         //Check if there is a queen on the right line of the current piece -         if(Col + i <8&& chessboard[Row] [col +i]) the             returnTRUE; the              the         //Check if the upper left corner of the current piece has a queen on the diagonal the         if(Row-i >=0&& col-i >=0  -&& chessboard[Row-i] [col-i]) the                 returnTRUE; the                  the         //Check the top right corner of the current pawn for a queen94         if(Row-i >=0&& Col + i <8  the&& chessboard[Row-i] [col +i]) the                 returnTRUE;  the     }98      About     //execute here to show no conflict, return false -     returnFALSE;101 }102 103 /*104 * * Function function: the * * Print Board106 */107 void108 Print_board ()109 { the     intRow;111     intCol; the     Static intSolution_count;//static variables, calculating the first few answers113Solution_count + +; theprintf"Solution%d:\n", solution_count); the      the     /*117 * * Traverse the board and print118     */119      for(row =0; Row <8; ++row) -     {121          for(col =0; Col <8; ++Col)122         {123             if(chessboard[row] [col])124printf"Q" ); the             Else126printf" +" );127         } -Putchar ('\ n' );129     } thePutchar ('\ n' );131}

Question of the eight Queens

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.