[LeetCode fl] N-Queens II, leetcoden-queens

Source: Internet
Author: User

[LeetCode fl] N-Queens II, leetcoden-queens

Question:

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

Answer:

The idea is simple, that is, the number of Queen N issues solved by brute force. The process is as follows:

The idea is not difficult. That is, the implementation of the code is complicated. EspeciallyBacktracking. HoweverRecursionThe idea is very simple. If the number of solutions for the Queen in column j of row I is represented as S (I, j ).

Total number of solutions= S (0, 0) + S (0, 1) +... + S (0, n)

= [S (1, 0, at this time (0, 0) There is a Queen) + S (1, 1, at this time (0, 0) There is a Queen) +...] + [S (1, 0, at this time (0, 1) there is a Queen) + S (1, 1, at this time (0, 1) there is a Queen) +...] +...

= ......

It is equivalent to drawing a huge tree, collecting the number of solutions for each slave node into the parent node, and then gathering the parent node into the parent node of the parent node ...... in this way, the root node is the total number of solutions, and the number of dead nodes in the middle is not counted.

If two-dimensional array is used to count, it will be very troublesome to execute. Here we will introduce a small trick, just using a one-dimensional array,Array I element j Represents placing a queen in column j of row I on the board(Or column I in row j of the Board. This doesn't matter ).

Similarly, we need a check function to determine whether the current placement of a queen is in conflict with the placement of the Queen. The judgment of rows and columns is very simple. The judgment of diagonal lines is a little complicated and requires the use of a property:

For the two vertices whose coordinates are (a, B) and (c, d ),Diagonal intersectionYesAbs (a-c) = abs (B-d).

Final code:

Class Solution {public: // determine whether it is in conflict with the previously filled queen. bool check (int * queen, int count) {for (int I = 0; I <count; ++ I) {if (queen [I] = queen [count] | abs (count-I) = abs (queen [count]-queen [I]) {return false ;}} return true ;}// recursive function int iterQueens (int * queen, int count, int row) {// The maximum valid value of row is (n-1 ), if = n, it indicates that if (count = row) return 1; int sum = 0; for (int col = 0; col <count; ++ col) has been completed) {queen [row] = col; if (check (queen, row) {sum = sum + iterQueens (queen, count, row + 1) ;}} return sum ;} int totalNQueens (int n) {int * queen = new int [n]; // initiatefor (int I = 0; I <n; ++ I) {queen [I] =-1;} return iterQueens (queen, n, 0 );}};

Non-recursive versions are much more difficult than this one. You can check the portal (it should be the correct solution, but I wrote it again but it is time-out and I don't know why ......)

Most importantly, one of the above connections is described.Bitwise operationMethod (I didn't understand it anyway, after all, it's a chicken dish), yesFastest.

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.