(Hdu step 4.3.6) N queen problem (solved by DFS), hdudfs

Source: Internet
Author: User

(Hdu step 4.3.6) N queen problem (solved by DFS), hdudfs

Question:

N queen's question
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 568 Accepted Submission (s): 332
 
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.
Output
There are several rows in total, each row has a positive integer, indicating the number of different places corresponding to the queen of the input row.
Sample Input
1850
 
Sample Output
19210
 
Authorcgf
Source2008 HZNU Programming Contest
Recommendlcy


Question Analysis:

This question is Queen N's question. The so-called N queen problem is nothing more than placing N pieces on a N * N board. Each piece is not in the same row, column, or diagonal line. Note that only one-dimensional array map [] is enabled in this question to store the visit situation of pawns. That is, in a certain dimension, he has already limited his placement (for example, in this question, the I-th piece must be placed in line I ).



The Code is as follows:

/** F. cpp ** Created on: February 25, 2015 * Author: Administrator */# include <iostream> # include <cstdio> using namespace std; const int maxn = 11; /*** map []: used to store the k-th Column. * Note that map [k] = j. Indicates that k pieces are placed in column j. * At the same time, it is in the k line */int map [maxn]; bool visited [maxn]; // It is used to mark whether a column has accessed int result [maxn]; // create a table and save the results bool flag; // used to mark whether a piece is successful int n; int ans; // when n is a specific value, number of placement schemes/*** Deep Search. * k: indicates the number of pieces currently placed */void dfs (int k) {if (k = n + 1) {// if k has reached n + 1. it indicates that all the pawns have been placed. ans ++; // number of successful pawns + 1 return;} int I; int j; for (I = 1; I <= n; ++ I) {// traverse all columns if (visited [I] = false) {// if this column is not accessed map [k] = I; // try to put the k pawns in column I (The k flag must be in row k) Flag = true; // ratio of the attempt to mark as successful for (j = 1; j <= k-1; ++ j) {// traverse the previous K-1 piece/*** if they are on the same slash (in this implementation, he has ensured that none of the pawns will be in different rows and columns. * At this time, we only need to determine whether they are on the same diagonal line.) */if (abs (map [k]-map [j]) = abs (k-j )) {flag = false; // set the flag to falsebreak;} if (flag = true) {// if flag is true, indicates that the k pieces can be placed successfully. visited [I] = true; // mark column I as having accessed dfs (k + 1 ); // start to visit the k + 1 chess piece visited [I] = false; // Remark column I as not accessed. used in other cases }}} int main () {int I ;/*** Case: */for (I = 1; I <= 10; ++ I) {memset (visited, false, sizeof (visited); flag = false; n = I; ans = 0; dfs (1); result [n] = ans;} while (scanf ("% d", & n )! = EOF, n) {printf ("% d \ n", result [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.