"Leetcode from zero single brush" N-queens II

Source: Internet
Author: User

Topic:

Follow up for n-queens problem.

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

Answer:

The idea is simple, is the brute force solution of the N queen problem count. The process is as follows:

    1. If the Queen is placed in column J of line I, then the Queen of the Line (i+1) does not contradict it, then the first line (i+2) ... ;
    2. If each column is not feasible, then we go back one line and proceed to the 1th step until it succeeds (3) or fails. Failed to retrace the line again and then to the 1th step;
    3. Success on the nth line also put the Queen, calculate a solution. The total number of solutions +1, back to the previous row, the number of columns +1, go to the 1th step again.

The train of thought is not difficult. Is that the implementation of the code is more complex. Especially the backtracking process. But if you take advantage of the idea of recursion , it's easy. If the number of solutions in column J of row I is the Queen, it is expressed as S (I, J).

Total Solution = 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) +. .] + ...

= ......

is equal to the drawing of a large tree, statistics of the number of each sub-leaf node solution to the parent node, the parent node is then pooled to the parent node ... As a result, the root node is the total number of solutions, and the dead nodes in the middle of the count will not be counted.

At this point, if you use a two-dimensional array to count, execution can be very cumbersome. Here is a little trick, just using a one-dimensional array, the array I element J represents a queen (or column I in the row J of a chessboard) in column J of the board I, which does not matter.

Similarly, we need a check function to determine whether the current placement of a queen will contradict the previous placement of the Queen. The judgment of the rows and columns is simple, and the diagonal judgment is slightly more complex and requires a property to be used:

For two points with coordinates (a, b) and (C, D), if the diagonal intersection will have abs (a-c) = = ABS (b-d) properties.

Final code:

Class Solution {public://Determines if there is a 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) {//row maximum valid value is (n-1), if = = N description already filled in board if (count = = row) return 1;int su m = 0;for (int col = 0; col < count; ++col) {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);};

For a non-recursive version, it's a lot more hassle than that. Can look at the portal (should be the correct solution, but I wrote it again but it is time-out do not know why ...) )

Most importantly, the above link describes a method of bit operation (I do not understand, after all, vegetable chicken), is the fastest way to solve.

"Leetcode from zero single brush" N-queens II

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.