HDU 2553 N Queen problem Classic search, DFS solution

Source: Internet
Author: User

N Queen's questionTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 10239 Accepted Submission (s): 4609


Problem description placed n Queens on the N*n's checkered chessboard, making them not attack each other (that is, any 2 queens are not allowed to be in the same row, in the same column, or in a diagonal line with a 45-angle checkerboard border.)
Your task is to find out how many legal placement methods are available for a given n.


Input has several lines, one positive integer n≤10 per line, indicating the number of boards and queens, or, if n=0, the end.
Output has several rows, one positive integer per line, representing the number of different placements of the queen corresponding to the input row.
Sample Input
1850

Sample Output
19210

Authorcgf
The code explains everything code:
#include <stdio.h> #define MAX 15int n, ans = 0, res[max],map[max];void DFS (int row) {if (row = n) {ans++; return;} for (int i = 0; i < n; ++i) {bool flag = false; Map[row] = i, for (int j = 0; j < row; ++j) {if (map[row]==map[j]| | map[row]-row==map[j]-j| | MAP[ROW]+ROW==MAP[J]+J) {flag = true; break;}} if (!flag) {DFS (row+1);}}} int main () {for (int i = 1; i <=; ++i) {n = i; ans = 0;D fs (0); Res[i] = ans;} while (~SCANF ("%d", &n) && N) {printf ("%d\n", Res[n]);} return 0;}


with June

HDU 2553 N Queen problem Classic search, DFS solution

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.