C++dfs Method Full Arrangement

Source: Internet
Author: User

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

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.