A few days ago to see Guile's "Ah ha!" Algorithm, in which the algorithm is said to be particularly popular and meticulous, is really a junior high school students can read the algorithm book (I was a sophomore to read: P). This code is very suitable for beginners of the algorithm students.
#include <iostream>using namespacestd;inta[Ten], book[Ten], N;//full permutation algorithm, using depth-first search DfsvoidDfsintStep) { inti; if(Step = = n +1)//If you stand in front of the n+1 box, it means that the first n boxes are lined up. { for(i =1; I <= N; i++)//output an arrangement (poker number in the 1-n box){cout<<A[i]; } cout<<Endl; return;//return to the previous step (where the DFS function was last called) } //What card should I put in front of step box?//Experiment 1, 2, 3, 4 ... for(i =1; I <= N; i++) { //Determine if poker I is still on hand if(Book[i] = =0) {A[step]= i;//Place Poker Number I in step a boxBook[i] =1;//set Book[i] to 1 to indicate that the card is not in handDfs (step+1);//Step one box and go to the next one .Book[i] =0;//and take back the poker you just tried. } } return;}intMain () {CIN>>N; DFS (1); return 0;}
C++dfs Method Full Arrangement