To summarize permutation and combination of algorithmic problems

What is the full array?

Full alignment (Full Permutation). This is generally the 1 to n n integers placed in a certain order according to a permutation of the result referred to the number n, and the full array refers to all permutations of the n integers are able to form.

For example 1,2,3 for the three integers

(1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), (3,2,1) is a full arrangement.

  So how to solve this kind of problem the whole arrangement? From the point of view to consider recursive, if the problem is described as "1 to n outputs full array of integers", then it may be divided into several sub-problems:

"Output 1 at the beginning of the full array" "output 2 beginning with the whole arrangement" ...... "is output to the beginning of the full array n." You may wish to set an array P, used to store the current arrangement; resetting a

Hash array hashTable, wherein hashTable [x] when the integer x is already on the array P as true. (After all, a full array of aligned only once each).

  Now in order to place the first bit to n number of P fill. Let's assume that this has been completed the P [1] to P [index-1], is ready to fill the P [index]. Obviously we need to enumerate 1 to n, if

Not current digital x P [1] to P [index-1] (i.e. hashTable [x] == false), then put it filled P [index], while hashTable [x] is set to true, then to deal with the first

index + 1 bits (i.e. recursive); when recursion is completed, then hashTable [x] reduction site false, so that P [index] fill the next number.

 

I simply drew what number of graphs as follows:

 

 

 code show as below:

. 1 #include <stdio.h>
 2  
. 3  
. 4  void generateP ( int index);  
 . 5  
. 6  const  int MAXN = . 11 ;
 . 7  
. 8  int n-;
 . 9  int P [MAXN], hashTable [MAXN] = { 0 };
 10  int main () {
 . 11      
12 is      int Array [ . 3 ] = { . 1 , 2 , . 3 };
 13 is      
14      n-= . 3 ;
 15      
16      // from No. 0 the currently processed array starts 
17     generateP(1); 
18     return 0;
19 } 
20 
21 
22 void generateP(int index){
23     
24     if(index == n+1){
25         for(int i = 1;i<=n;i++){
26             printf("%d ",P[i]);
27         }
28         printf("\n");
29         //return;
30     } 
31 is      
32      // recursive member 
33 is      for ( int x = 1 ; x <= n-; x ++) { // Enumeration 1 to n, an attempt to fill x P [index] 
34 is          IF (hashTable [x] == 0 ) { // if x is not P [0] to P [index-1] in the 
35              P [index] = x;     // make the first bit index P is x, i.e. x is added to the current arrangement 
36              hashTable [x] = . 1 ; // record x has a (reusable) in P 
37 [              generateP (+ index . 1 );     // No. index + 1 bit processing arrangement 
38 is              hashTable [x] = 0 ;      //Has been processed P [index] x of the sub-problems, a reduced state 
39          }
 40      } 
 41 }

You can see the output results are as follows:

 

 

Before this idea and we are in agreement. I.e., to consider the recursive perspective, "output to the beginning of the full array 1", "output to the beginning of the full array 2", "3 begins with the output of the whole arrangement."

Another way

 

 

 1 #include <stdio.h>
 2 
 3 
 4 
 5 
 6 void swap(int A[],int i,int j);
 7 void printArray(int A[],int n);
 8 void perm(int A[],int p,int q);
 9 
10 int main(){
11     
12     int A[3] = {1,2,3};
13     perm(A,0,3);
14     
15     return 0;
16 }
17 
18 void swap(int A[],int i,int j){
19     int temp = A[i];
20     A[i] = A[j];
21     A[j] = temp;
22 }
23 
24 void printArray(int A[],int n){
25     for(int i = 0;i < n;i++){
26         printf("%d ",A[i]);
27     }
28     printf("\n");
29 }
30 
31 void perm(int A[],int p,int q){
32     if(p == q){
33         printArray(A,q);
34     }
35     
36     for(int i = p;i <q;i++){
37         swap(A,p,i);
38         perm(A,p+1,q);
39         swap(A,p,i);  
40     }
41 }

 

Guess you like

Origin www.cnblogs.com/ManOK/p/12543714.html