String arrangement and eight queens question

Source: Internet
Author: User

string permutation problems

Enter a string that prints all the permutations of the string. If you enter ABC, the output is ABC,ACB,BCA,CAAB,CBA. Based on the knowledge of permutation combinations, 3 strings are arranged in 3!=6.

Consider the case where the character is not duplicated first:

You can divide a character into two parts, the first part is the first character, and the second part is the back section.

So the arrangement of ABC can be analyzed in this way:

A+BC;

B+ac;

C+ab;

The first character is exchanged for each character at a time, and then the remainder is arranged separately. This thought turns the problem of n into a n-1 problem. To implement with recursion:

Here is the code on the point of the sword:

/* Enter a string to print all the permutations of the characters in the string, many other questions * can be converted to this question, such as how to make the corresponding surface of the cube on the same number and the same, the eight queen question etc. */void strpermutation (char * Origin,char * Pbegin); void  strpermutation (char * origin) {if (origin==null) return; Strpermutation (Origin,origin);} Origin pointer to the beginning of the string, Pbegin points to the pointer that needs to be modified each time void strpermutation (char * Origin,char *pbegin) {if (*pbegin== '} ') {Std::cout <<origin<<std::endl;} Else{for (char *pch=pbegin;*pch!= ';p ch++) {char Temp=*pch;*pch=*pbegin;*pbegin=temp;strpermutation (Origin, pbegin+1); temp=*pch;*pch=*pbegin;*pbegin=temp;}}} int main () {std::cout<< "string permutation problem" <<std::endl;char  s[]= "abc"; strpermutation (s); return 0;}

The equal and eight queens of the 4 vertices on the opposite surface of three groups on a cube

After solving this problem, a lot of other problems can be translated into this problem, the following two questions are also the topic of the sword point:

Enter an array of 8 numbers to determine if it is possible to place the 8 numbers on the 8 vertices of the cube, making the equals of the 4 vertices on the opposite sides of the three groups on the cube.


This question actually asks whether there is a a1-a8 such a combination that makes a1+a2+a3+a4==a5+a6+a7+a8,a1+a3+a5+a7==a2+a4+a6+8,a1+a2+a5+a6==a3+a4+a7+a8, This is only two points different from the arrangement of the above string:

1. Depending on the data type, the string can determine whether the string ends in terms of ' + ', and an array of numbers requires a parameter that represents the length of the array.

2. Not all permutations need to be printed out, only those that meet the above conditions need to be printed out.

In the same vein, the question of the eight Queens is basically the same as the vertex and equality of the three groups on the Upper cube.

The eight Queens question is as follows:

Put 8 queens on 8*8 's chess plate so that they cannot attack each other, that is, any two queens must not be in the same row, the same column, or agree on the diagonal. So what are the conditions of the pendulum?

Since 8 queens cannot appear on the same line, then the pit is a single row for each queen, then you can define an array of columnindex[8], the number I in the array represents the column number of the Queen in line I, first initialize the array columnindex with 1-8 eight digits, The array is then fully arranged. Since an array is initialized with a different number, any two queens must not be in the same column, so it is only necessary to judge whether any two queens are on the same diagonal. and I-j==columnindex[i]-columnindex[j] or i-j==columnindex[j]-columnindex[i] can be established. This is the judging condition of the eight Queen's question.

Understand the difference after the code is very easy, the change is also very small: wherein Judege is to judge the three groups opposite the vertex and whether the equivalent of the function, Eightqueen is to determine whether the function of 8 queen position, only need to change in the numpermutation under the conditions of judgment, The output is the solution that meets the criteria.

#include "util.h" void numpermutation (int *num,int *pbegin,int len);//The judgment function of the equality of three groups of objects in a cube bool judge (int *num,int len);// combo bool Eightqueen (int *num,int len) with 8 queens position, void numpermutation (int * Num,int len) {if (num==null) return; numpermutation (Num,num,len);} void numpermutation (int *num,int *pbegin,int len) {if ((pbegin-num) ==len) {if (judge (Num,len)) {//Print the contents of an array Printarrayt ( Num,len);}} Else{for (int* i=pbegin;i<pbegin+len-(pbegin-num); i++) {int tmp=*pbegin;*pbegin=*i;*i=tmp;numpermutation (num, Pbegin+1,len); tmp=*i;*i=*pbegin;*pbegin=tmp;}}} BOOL Judge (int *num,int len) {if (num==null| | LEN!=8) return False;if ((num[0]+num[1]+num[2]+num[3]) = = (Num[4]+num[5]+num[6]+num[7]) && (num[0]+num[2]+num [4]+num[6]) = = (Num[1]+num[3]+num[5]+num[7]) && (num[0]+num[1]+num[4]+num[5]) = = (num[2]+num[3]+num[6]+num[7 ]) return True;return false;} BOOL Eightqueen (int *num,int len) {if (num==null| | LEN!=8) return false;for (int i=0;i<len;i++) {for (int j=0;j<len;j++) {if (i!=j) {if (i-j) = = (Num[i]-num[j]) | | (i-j) = = (Num[j]-num[i]) return false;}}} return true;} int main () {int arr[]={1,2,3,4,5,6,7,8};numpermutation (arr,sizeof (arr)/sizeof (arr[0])); return 0;}

Print array Template<class t>void printarrayt (T * T,int len) {for (int i=0;i<len;i++) std::cout<<t[i]<< " "; Std::cout<<std::endl;}


Eliminate repeating characters

These are the cases where there are no duplicate characters in the string discussed, if there are repeating characters in the string, or if the idea above, it should be in some cases not recursive query

Example with character ABBC:

First, the character is divided into two parts, the first character and the remaining characters, and then the first character is followed by the following character interchange

A+bbc

B+abc

B+bac

C+bba

There will be duplicates in the 2nd and 3rd of the above 4 alternatives, and the BAC arrangement is the same in the 2nd Chinese program (This is the whole permutation problem of the characters without repetition, there is a 3!=6 arrangement scheme), then the 3rd scheme can be discarded. The general term is that if there are characters that are the same as the characters being replaced, you do not need to replace the character and solve the whole arrangement. The diagram is as follows:


The code is as follows:

#include <iostream> #include <stdlib.h> #include <stdio.h>/* Enter a string to print all permutations of characters in the string, many other problems * Can be converted to this question, such as how to make the corresponding surface of the cube and the same, the eight Queen question, such as *///to determine whether the function of the exchange of bool Is_swap (char * Begin,char *end);//Swap two pointers to the variable's function void Swap (char * P1,char *P2);//string permutation function void strpermutation (char * Origin,char *pbegin); void Strpermutation (char * origin) {if (origin== NULL) return; Strpermutation (Origin,origin);} Origin pointer to the beginning of the string, Pbegin points to the pointer that needs to be modified each time void strpermutation (char * Origin,char *pbegin) {if (*pbegin== '} ') {Std::cout <<origin<<std::endl;} Else{//pbegin points to only one part of the element in the string, and the pCh points to the remainder of the string for (char *pch=pbegin;*pch!= ';p ch++) {if (Is_swap (pbegin,pch)) { Swap (PBEGIN,PCH);//Exchange Pbegin and Pchstrpermutation (origin,pbegin+1); swap (pbegin,pch);//Exchange Pbegin and pch}}}}//to determine if the exchange, if [ Begin,end) interval exists with the end pointer pointing to the same value, and returning false indicates that there is no need to swap bool Is_swap (char * begin,char * end) {bool Flag=true;for (char *p=begin;p <end;p++) {if (*p==*end) Flag=false;} return flag;} Swaps the value of two pointers to void swap (char * P1,char *p2) {char tmp=*p1;*p1=*p2;*p2=tmp;} int main() {std::cout<< "de-duplicated string permutation problem" <<std::endl;char s[]= "ABBC"; strpermutation (s); return 0;} 
ABBC the number of all combinations is calculated as a44/a22=12 with a mathematical formula, and the output is 12 groups of non-repeating permutations.

String arrangement and eight queens question

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.