From full sorting of characters to eight queens

Source: Internet
Author: User

Assume that the full sorting of the string/array is required. For example, the full sorting of "123" has three options: 1st bits, 2nd bits, and 3rd bits, the time complexity is obviously 3 !. In fact, it is easy to know that for n characters, a total of N are in full order! Possible value: n! Time complexity. we need to find an algorithm to find all possible sorting. The best method is recursion. If loop is used, N layers of nesting are required, which is not realistic. assume that we already have a function fullpermutation (A [n]) that can sort the array a [n] in full order. Therefore, we can divide a [n] into two subproblems: A [1] and a [n-1]. Among them, a [1] can have n options: exchange a [1] with a [I], where I = 1, 2, 3, 4 ....... remember to recover after switching. the full sorting of a [n-1] can be completed by calling fullpermutation.
Recurrence formula:
F (n): = done n = 1
= N * F (n-1) n> 1

The above analysis and recursive formula make it easy to write code:

# Include <iostream> using namespace STD; void full_permutation (int * a, int beg, int end) {If (beg = END) {for (INT I = 0; I <= end; ++ I) cout <A [I] <''; cout <Endl; return ;}for (INT I = beg; I <= end; ++ I) // A [1] has n options and requires N cycles {swap (A [beg], a [I]); // select the beg value full_permutation (A, beg + 1, end); // solve n-1 subproblem swap (A [beg], a [I]);} int main () {int A [8] = {,}; full_permutation (A,); Return 0 ;}

Extended: the time complexity is (N !), We are easy to think of the n queen problem. The most classic eight queen problem: Eight queens are placed on 8 × 8 chess sets so that they cannot attack each other, that is, the two queens are not allowed to be on the same row, column, or diagonal slashes. We can start with this analysis. The 1st queens can select 8 positions on the 10 th row, you can select 7 locations for the 2nd queens in the 2nd rows (as long as they are not in the same column as the 1st queens ), you can select 6 locations for the first 3rd queens on the second row (as long as they are not in the same column as the second and second queens ). if we do not consider the diagonal line attack direction, we can have n queen issues! Same as the full arrangement above. Similarly, we can consider n of rows and columns first! In all cases, and then eliminate the diagonal line conflict, and then narrow down the scope, you can find out the solution of the eight queens. based on this idea, the eight queens problem is not difficult. now, we have the last question? What data structure is used to save the two-dimensional coordinates of the queen? In fact, it is easy to use (I, a [I]) to save the location of the queen, that is, an array, and index I to represent rows, A [I] indicates a column. define a "A [8]" to represent the eight queens.

The Code is as follows:

# Include <iostream> using namespace STD; bool islegalqueens (int * a, int end) {for (INT I = 0; I <= end; ++ I) {for (Int J = I + 1; j <= end; ++ J) {if (a [J]-A [I] = J-I | A [J]-A [I] = I-j) // If the slope of two points is 1 or-1, return false is invalid;} return true;} void full_permutation (int * a, int beg, int end) {If (beg = END) {If (islegalqueens (A, end) {for (INT I = 0; I <= end; ++ I) cout <A [I] <''; cout <Endl;} return;} For (INT I = beg; I <= end; ++ I) // A [1] has n options and requires N cycles {swap (A [beg], a [I]); // select the beg value full_permutation (, beg + 1, end); // solve n-1 subproblem swap (A [beg], a [I]);} int main () {int A [8] = {,}; full_permutation (A,); 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.