POJ3239-Solution to the N queens puzzle

Source: Internet
Author: User

Reprinted please indicate the source: Thank youHttp://blog.csdn.net/lyy289065406/article/details/6642789

General question:

Expansion of Queen eight: Question of Queen n

PS: I used the traditional DFS backtracking and constructor methods to solve the problem.

Solution:

First, let's talk about the concept of traditional tracing DFS:

Add the Queen row by row, mark the columns that have been filled, and use the slope to mark the two oblique edges, when the slope between the point to be filled and any filled point is 1 or-1, this position cannot be filled

 

Although it is the expansion of Queen eight, it is much more difficult to make such expansion...

The N value range of the question is 8 to 300, and the time is limited to 1000 ms. Traditional DFS backtracking cannot be used. If the N value exceeds 30, the problem will be solved by the traditional method.

 

This problem can only be solved through mathematical methods, that is, to find a rule for placing n queens in all the boards of different sizes, and use mathematical expressions or sequencesStructureThis rule

Generally, after the formula is successfully constructed, the question is very watery.ConstructorThe worst complexity is O (n), and N is only 300 ....

But the difficulty is how to deduce the formula of this "regular...

 

I personally think it is necessary to draw dozens of boards of different sizes by hand to see a little eye... It is too challenging. If you are interested, try it manually...

I used online formulas (sequences) to answer this question. To be honest, I admire people who have found this rule, I think this formula can be used as a special principle to solve the problem of Queen n...

 

Constructor formula (sequence ):
1. When n mod 6! = 2 or N mod 6! = 3:

[,..., N], [,..., n-1] (N is an even number)

[,..., N-1], [,..., n] (N is an odd number)

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], [, 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, n is an even number)

[K, K + 2, K + 4 ,..., n-2], [, 5 ,..., k-2], [K + 3 ,..., n-1], [2, 4 ,..., k + 1], [N] (K is an odd number, n is an odd number)

 

(There are six sequences above. One sequence in a row, and the brackets are added to help you identify the subsequence. The subsequence and the subsequence are in a continuous relationship. Ignore the brackets. The number of I is Ai, indicating that there is a queen in the AI column of row I; In the sequence omitted by..., the numbers of adjacent two increase by 2 .)

 

 

/* Code 1: constructor * // memory time // 188 K 16 Ms # include <iostream> # include <cmath> using namespace STD; int main (int I) {int N; // Number of Queens while (CIN> N) {If (! N) break; If (N % 6! = 2 & N % 6! = 3) {If (N % 2 = 0) // n is an even number {for (I = 2; I <= N; I + = 2) cout <I <''; for (I = 1; I <= n-1; I + = 2) cout <I <''; cout <Endl ;} else // n is an odd number {for (I = 2; I <= n-1; I + = 2) cout <I <''; for (I = 1; I <= N; I + = 2) cout <I <''; cout <Endl ;}} else if (N % 6 = 2 | n % 6 = 3) {If (N % 2 = 0) // n is an even number {int K = n/2; If (K % 2 = 0) // K is an even number {for (I = K; I <= N; I + = 2) cout <I <''; for (I = 2; I <= K-2; I + = 2) cout <I <''; for (I = K + 3; I <= n-1; I + = 2) cout <I <''; for (I = 1; I <= k + 1; I + = 2) cout <I <''; cout <Endl;} else // K is an odd number {for (I = K; I <= n-1; I + = 2) cout <I <''; for (I = 1; I <= K-2; I + = 2) cout <I <''; for (I = K + 3; I <= N; I + = 2) cout <I <''; for (I = 2; I <= k + 1; I + = 2) cout <I <''; cout <Endl ;}} else // n is an odd number {int K = (n-1)/2; if (K % 2 = 0) // K is an even number {for (I = K; I <= n-1; I + = 2) cout <I <''; for (I = 2; I <= K-2; I + = 2) cout <I <''; for (I = K + 3; I <= n-2; I + = 2) cout <I <''; for (I = 1; I <= k + 1; I + = 2) cout <I <''; cout <n <Endl;} else // K is an odd number {for (I = K; I <= n-2; I + = 2) cout <I <''; for (I = 1; I <= K-2; I + = 2) cout <I <''; for (I = K + 3; I <= n-1; I + = 2) cout <I <''; for (I = 2; I <= k + 1; I + = 2) cout <I <''; cout <n <Endl ;}}} return 0 ;}

 

============ Gorgeous split line ============

 

 

/* Traditional DFS backtracing TLE (N is difficult to search if n exceeds 30) * // traditional DFS backtracing TLE (if n exceeds 30, it fails) # include <iostream> # include <cmath> using namespace STD; typedef class {public: int R, C;} location; location POS [301]; // mark the filled Queen coordinate bool Col [301]; // mark int N in the column; // Queen number bool DFS (int x, int P) {If (P = N) return true; bool sign; For (INT y = 1; y <= N; y ++) // enumerate each column of X in the current row {bool flag = false; For (INT I = 1; I <= P; I ++) // enumerate every filled queen if (ABS (X-pos [I]. r) = ABS (Y-pos [I]. c) // check the position (X, Y) Is it on their Oblique Edge? {// It is actually marking the slope. If the slope is equal to 1 or-1, it is not allowed to fill in the Queen flag = true; break;} If (! Flag &&! Col [y]) {Col [y] = true; POS [p + 1]. R = x; POS [p + 1]. C = y; Sign = DFS (x + 1, p + 1); If (sign) return true; elsecol [y] = false; // backtracking, the POS [] record will be automatically overwritten by the new value, so you do not need to specifically handle} return false; // y of all columns in the current row X is not allowed to be placed in the queen, description: The first line is incorrectly entered.} int main (void) {While (CIN> N) {If (! N) break; int p = 0; // POS [] pointer, number of filled pieces memset (COL, false, sizeof (COL); DFS (1, P ); for (INT I = 1; I <= N; I ++) cout <POS [I]. c <''; cout <Endl;} 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.