HDU 2553 n queen problem (deep recursive search)

Source: Internet
Author: User
N queen's question

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3730 accepted submission (s): 1737

Problem description places n queens on the square board of N * n so that they do not attack each other (that is, two queens are not allowed to be in the same row, the same column, it is not allowed to be on a diagonal line with 45 corners of the checker border.
Your task is to determine the number of valid placement methods for the given n.

 

There are several input rows. Each row has a positive integer of N ≤ 10, indicating the number of the Board and the Queen. If n = 0, it indicates the end.

 

There are several rows in output. Each row has a positive integer, indicating the number of different places corresponding to the queen of the input row.

 

Sample input1
8
5
0

 

Sample output1
92
10
 /*  * N queen problem, because N is a positive integer less than or equal to 10, you can use the table-hitting method to locate all the top 10 cases and then do not re-calculate each input.  */  # Include <Stdio. h> # Define Nums 10 /*  Number entered: 1---10  */  Int  N;  /*  Chessboard  */  Int Chessboard [ 11 ] [ 11  ];  /*  Used to record the number of visits  */  Int  Cal;  /*  Check if this column is allowed for the Queen to place this row. 1 is returned, and 0 is not returned. This recursion shows a row and K is the length of the Board.  */  Int Dfs_check ( Int Row, Int Column, Int  K ){  /* It indicates that the front and back sides of the checker have reached the external boundaries.  */      If (Row> K) {Cal ++ ;  Return   1  ;}  /*  Is there a queen on the top?  */      For ( Int I = 1 ; I <row; I ++ ) /*  If there is a queen, the return value cannot be placed. Here, the return value is 0.  */      If (Chessboard [I] [column] = 1  )  Return   0  ;  /*  Check whether the 45-degree angle in the top left and right corner is acceptable  */      /*  Upper left  */      For (Int I = row- 1 , J = column- 1 ; I> 0 & J> 0 ; I --, j -- )  If (Chessboard [I] [J] = 1  )  Return   0  ;  /*  Upper right  */     For ( Int I = row- 1 , J = column + 1 ; I> 0 & J <= K; I --, J ++ )  If (Chessboard [I] [J] = 1  )  Return   0  ;  /*  The location is marked successfully.  */ Chessboard [row] [column] = 1  ;  /*  Search for the next row  */      For ( Int I = 1 ; I <= K; I ++ )  If (Dfs_check (row + 1 , I, K) = 1  )  Break ; Chessboard [row] [column] = 0  ;  Return   0  ;}  Int  Main (){  Int  I, J, K;  Int Count [ 11  ];  /*  Table Creation  */     For (K = 1 ; K <= Nums; k ++ ) {Count [k] = 0  ; Cal = 0  ;  /*  First, set all checkerboard initialization to 0.  */          For (I = 0 ; I <= Nums; I ++ )  For (J = 0 ; J <= Nums; j ++ ) Chessboard [I] [J] = 0  ;  For (I = 1 ; I <= K; I ++ ) Dfs_check (  1  , I, K); count [k] = Cal ;}  While (Scanf ( "  % D  " , & N )! = EOF & n! = 0 ) Printf (  "  % D \ n  "  , Count [N]);  Return   0  ;} 

 

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.