HDU2553 n Queen question "backtracking method"

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): 9579 Accepted Submission (s): 4314
Problem Description
N-Queens are placed in the N*n's checkered checkerboard, 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
There are several lines, one positive integer n≤10 per line, indicating the number of boards and queens, or, if n=0, the end.

Output
A number of rows, one positive integer per line, representing the number of different placements of the queen corresponding to the input row.

Sample Input
1
8
5
0

Sample Output
1
92

10


Title: N-Queens on the N*n board, n Queens cannot appear on the same line, in the same column, or in the same diagonal line.

Idea: Direct enumeration judgment is too slow, considering that each row has only one queen per column, then using an array of c[x] represents the first

The column number where the Queen is placed on the X row, that is, X for the row, and C[x] for the column. Judging whether the clash with the Queen of the Front can

Determines whether the current line cur is in conflict with the previous 0~j line.

C[cur] = = C[j] | | Cur-c[cur] = = J-c[j] | | Cur+c[cur] = = J+c[j] respectively determine whether in the same column, the same

The main diagonal, on the same diagonal.

However, you can continue to optimize. Directly with a vis[3][?] Directly determine if the column and diagonal of the currently trying queen are

There are other queens, that is,!vis[0][i] in the column whether there are other queens,!vis[1][cur+i] where the vice-diagonal has other royal

!vis[2][cur-i+n] There are other queens on the sub-diagonal.

But this commits the code also timed out ...

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;int vis[3][50],c[30],tot,n;void Search (int cur) {    if (cur = = N)    {        tot++;    }    else    {for        (int i = 0; i < N; i++)        {            if (!vis[0][i] &&!vis[1][cur+i] &&!vis[2][cur-i+n ])            {                Vis[0][i] = vis[1][cur+i] = Vis[2][cur-i+n] = 1;                Search (cur+1);                Vis[0][i] = Vis[1][cur+i] = Vis[2][cur-i+n] = 0;}}}    int main () {    while (~scanf ("%d", &n) && N)    {        tot = 0;        memset (vis,0,sizeof (Vis));        Search (0);        printf ("%d\n", tot);    }    return 0;}

Make a decisive statement

#include <iostream>using namespace Std;int main () {    int a[23] = {0,1,0,0,2,10,4,40,92,352,724};    int N;    while (CIN >> N)    {        if (n==0) break            ;        else            cout << a[n] << Endl;    }}




HDU2553 n Queen question "backtracking method"

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.