Full DFS Arrangement

Source: Internet
Author: User

# Include <iostream>
# Include <cstring>
Using namespace STD;
Char chars [] = {'A', 'B', 'C '};
Char result [3];
Int flag [3]; // used to mark whether it has been used
Int Len;
// Note that for printing, this function must accept an array of results to record that the character is put in each recursion.
Void show (INT index)
{
Int I;
For (I = 0; I <Len; I ++)
Cout <result [I] <"";
Cout <Endl;
}
Void DFS (INT index)
{
Int I;
If (Index = Len)
Show (LEN); // locate the result and print it
For (I = 0; I <Len; I ++)
If (! Flag [I]) // never used
{
Flag [I] = 1; // mark as used
Result [Index] = chars [I]; // enter the result of this query
DFS (index + 1); // find the next
Flag [I] = 0; // backtracking
}
// When the function comes out of the for loop, it proves that all the possibilities of the current one have been found, and the previous recursion will be returned.
}

Int main ()
{
Memset (flag, 0, sizeof (FLAG ));
Len = sizeof (chars)/sizeof (char );
DFS (0 );
Return 0;
}

Full DFS Arrangement

Related Article

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.