Poj 3239 solution to the N queens puzzle

Source: Internet
Author: User

1. Link:

Http://poj.org/problem? Id = 3239

2. content:

Solution to N Queens puzzle
Time limit:1000 ms   Memory limit:131072 K
Total submissions:3459   Accepted:1273   Special Judge

Description

The eight queens puzzle is the problem of putting eight chess queens on an 8 × 8 chessboard such that none of them is able to capture any other. The puzzle has been generalized to arbitraryN×NBoards. GivenN, You are to find a solution toNQueens puzzle.

Input

The input contains multiple test cases. Each test case consists of a single integerNBetween 8 and 300 (inclusive). A zero indicates the end of input.

Output

For each test case, output your solution on one line. The solution is a permutation of {1, 2 ,...,N}. The number inITh place meansITh-column queen in placed in the row with that number.

Sample Input

80

Sample output

5 3 1 6 8 2 4 7

Source

Poj monthly -- 2007.06.03, Yao, Jinyu

3. Method:

At the beginning, the 8 Queen's method was used, and it could not be found.

Only by searching, you can use the constructor method, and you cannot think of the constructor. Therefore, someone else's constructor formula is directly applied.

I don't think it makes sense. I submitted it directly using someone else's code. It's also a question.

Constructor:

Http://www.cnblogs.com/rainydays/archive/2011/07/12/2104336.html

1. When n mod 6! = 2 and N mod 6! = 3, there is a solution:
2, 4, 6, 8,..., n, 1, 3, 5, 7,..., N-1 (N is an even number)
2, 4, 6, 8,..., n-1, 1, 3, 5, 7,..., n (n is an odd number)
(The number of I in the above sequence is Ai, indicating that there is a queen in the AI column in the line I; In the sequence omitted by..., the numbers of adjacent two increase by 2. Same below)
2. When n mod 6 = 2 or N mod 6 = 3,
(When n is an even number, K = n/2; when n is an odd number, k = (n-1)/2)
K, K + 2, K + 4 ,..., n, 2, 4 ,..., k-2, K + 3, K + 5 ,..., n-1, 1, 3, 5 ,..., k + 1 (k is an even number, n is an even number)
K, K + 2, K + 4 ,..., n-1, 2, 4 ,..., k-2, K + 3, K + 5 ,..., n-2, 1,3, 5 ,..., k + 1, n (K is an even number, n is an odd number)
K, K + 2, K + 4 ,..., n-1, 1, 3, 5 ,..., k-2, K + 3 ,..., n, 2, 4 ,..., k + 1 (k is an odd number and N is an even number)
K, K + 2, K + 4 ,..., n-2, 1,3, 5 ,..., k-2, K + 3 ,..., n-1, 2, 4 ,..., k + 1, n (K is an odd number, n is an odd number)

In the second case, it can be considered that when n is an odd number, the last piece occupies the last position of the last row, and then the n-1 piece is used to fill the n-1 board, in this way, the problem is converted to the same type and N is an even number.

If K is an odd number, the first half of the sequence is an odd number. Otherwise, the first half is an even number.

4. Code:

Http://blog.csdn.net/lyy289065406/article/details/6642789? Reload

 1 /*代码一:构造法*/ 2  3 //Memory Time  4 //188K   16MS  5  6 #include<iostream> 7 #include<cmath> 8 using namespace std; 9 10 int main(int i)11 {12     int n;  //皇后数13     while(cin>>n)14     {15         if(!n)16             break;17 18         if(n%6!=2 && n%6!=3)19         {20             if(n%2==0)  //n为偶数21             {22                 for(i=2;i<=n;i+=2)23                     cout<<i<<‘ ‘;24                 for(i=1;i<=n-1;i+=2)25                     cout<<i<<‘ ‘;26                 cout<<endl;27             }28             else   //n为奇数29             {30                 for(i=2;i<=n-1;i+=2)31                     cout<<i<<‘ ‘;32                 for(i=1;i<=n;i+=2)33                     cout<<i<<‘ ‘;34                 cout<<endl;35             }36         }37         else if(n%6==2 || n%6==3)38         {39             if(n%2==0)  //n为偶数40             {41                 int k=n/2;42                 if(k%2==0)  //k为偶数43                 {44                     for(i=k;i<=n;i+=2)45                         cout<<i<<‘ ‘;46                     for(i=2;i<=k-2;i+=2)47                         cout<<i<<‘ ‘;48                     for(i=k+3;i<=n-1;i+=2)49                         cout<<i<<‘ ‘;50                     for(i=1;i<=k+1;i+=2)51                         cout<<i<<‘ ‘;52                     cout<<endl;53                 }54                 else  //k为奇数55                 {56                     for(i=k;i<=n-1;i+=2)57                         cout<<i<<‘ ‘;58                     for(i=1;i<=k-2;i+=2)59                         cout<<i<<‘ ‘;60                     for(i=k+3;i<=n;i+=2)61                         cout<<i<<‘ ‘;62                     for(i=2;i<=k+1;i+=2)63                         cout<<i<<‘ ‘;64                     cout<<endl;65                 }66             }67             else   //n为奇数68             {69                 int k=(n-1)/2;70                 if(k%2==0)  //k为偶数71                 {72                     for(i=k;i<=n-1;i+=2)73                         cout<<i<<‘ ‘;74                     for(i=2;i<=k-2;i+=2)75                         cout<<i<<‘ ‘;76                     for(i=k+3;i<=n-2;i+=2)77                         cout<<i<<‘ ‘;78                     for(i=1;i<=k+1;i+=2)79                         cout<<i<<‘ ‘;80                     cout<<n<<endl;81                 }82                 else  //k为奇数83                 {84                     for(i=k;i<=n-2;i+=2)85                         cout<<i<<‘ ‘;86                     for(i=1;i<=k-2;i+=2)87                         cout<<i<<‘ ‘;88                     for(i=k+3;i<=n-1;i+=2)89                         cout<<i<<‘ ‘;90                     for(i=2;i<=k+1;i+=2)91                         cout<<i<<‘ ‘;92                     cout<<n<<endl;93                 }94             }95         }96     }97     return 0;98 }

 

Poj 3239 solution to the N queens puzzle

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.