C Language solution eight Queen question

Source: Internet
Author: User

1#include <stdio.h>2#include <stdlib.h>3 4 /*This code was used to cope with the problem of the eight queens.5 * Array Borad[9][9] is a virtual borad.6 * Line 0 and volumn 0 are ignored.7 * At first we find a place to set a queen on it and then mark this queen ' s8 * domain. The marking domain is add 1 on the board[line][volumn],9 * So the number of Board[line][volumn] means this place can attacked byTen * How many queens. After we mark this Queen's domain, we move on to next line. One * If there is no next line, we got a solution*/ A /*These codes are mainly used to solve the eight queens problem. - * Array Borad[9][9] is a virtual chessboard.  - * No. 0 row and No. 0 column are ignored the * At the very beginning, we found a place on a line, put the Queen, we put the Queen to - * The range of attacks is marked up. The way we mark it is to add 1 to the board[[column], - * So the value on the board[[column] means how many queens the position can be attacked by. - * Once we've marked the Queen's range, we'll jump to the next line.  + * If there's no next line, then we'll find a way to put it.*/ -  + /*If a place on board has been marked by NONE A * It means no Queen can attack this place, it's a potential place to set a queen*/ at /*if a position on the board is set to none, it means that no queen can attack the position, and this position can be set to Queen*/ - #defineNONE (0) - #defineQUEEN (-1) -  - #defineOCCUPY (1) - #defineDisoccupy (0) in  - voidCompute (intLine ); to voidPrintborad (void) ; + voidOperate_borad (intLineintVolumn,intfunction); -  the intMainvoid) * { $Compute (1) ;Panax Notoginseng     return 0 ; - } the  + /*ignored The first line and the first volumn*/ A intborad[9][9] ; the  + /*This is a recursive function. - * It'll find a queen on the line, $ * Then fine next one of the next line by call itself*/ $ /*This is a recursive function. Find the Queen's position on a line and call yourself to find the queen of the next line*/ - voidCompute (intLine ) - { the     inti; -     Static intNum_of_solution =0;Wuyi      the      for(i =1; I <=8; i++) -         if(Borad[line][i] = =NONE) Wu         { - Operate_borad (line, I, OCCUPY);  About             if(line = =8)//Find a solution $             { -printf"%d\n", ++num_of_solution); - Printborad (); -             } A             ElseCompute (line +1) ;/*contine to next line*/ /*find the Queen of a row*/ + Operate_borad (line, I, disoccupy); the         } - } $  the /*function: the * If function is OCCUPY and then set a flag of Queen on Borad[line][volumn] the * and set a flag on the domain of this queen the * If function is disoccupy, then clear flag of Queen on Borad[line][volumn] - * and clear the flag on the domain of this queen*/ in /*Features: the * If the function variable is occupy, then put the Queen on Board[line][volumn], the * and set the range of the Queen's attack About * If the function variable is disoccupy, remove the Queen from the Board[line][volumn] the * and clear the flag set by the Queen in its range of attack*/ the voidOperate_borad (intLineintVolumn,intfunction) the { +     intI, J, *temp, NL, NV; -      theBorad[line][volumn] = (function = = OCCUPY?)queen:none);Bayi  the     /*I and J is used to decide the direction*/ the      for(i =-1; I <=1; i++) -          for(j =-1; J <=1; J + +) -             if(i = =0&& J = =0)Continue ; the             Else the              for(nl = Line +i, NV = volumn +J; theNL >=1&& NL <=8&& NV >=1&& NV <=8 ; theNL + = i, NV + =j) -             { thetemp = &BORAD[NL][NV]; the                 /*Add one means this place can is attack by another queen*/ thefunction = = OCCUPY? (*temp) + +: (*temp)-- ;94             } the } the /*Function:print The Board on the screen*/ the /*Print Board*/98 voidPrintborad (void) About { -     intx, y, chess;101 102      for(x =1; X <=8; X + +)103     {104          for(y =1; Y <=8; y++) the         {106Chess =Borad[x][y];107             if(Chess! =QUEEN)108Putchar ('*') ;109             Else thePutchar ('Q') ;111         } thePutchar ('\ n') ;113     } the}

C Language solution eight Queen 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.