Algorithm exercise--C # DFS full permutation algorithm

Source: Internet
Author: User

void Main () {var r = A (new list<string> () {"A", "B", "C", "D", "E", "F"}); Console.WriteLine (R.count); Console.WriteLine (r);} 1. Loop each ELEMENT//2. Take the out first,2nd. Element//3. If After_take_out. Length > 1 then RECURSIVE//4. Get child result then combine into new resultstatic list<string> A (list<string> arr) {if (arr. Count <= 1) {return arr;} if (arr. Count = = 2) {return new list<string> () {arr[0]+ "," +arr[1],arr[1]+ "," +arr[0]};} var ret = new list<string> (); for (var i = 0;i < arr. Count;i + +) {var tmp = new list<string> (arr); var takeout = Arr[i];arr. RemoveAt (i); if (arr. Count > 1) {var childr = A (arr); var r = GetResult (takeout, CHILDR); ret. AddRange (R);}} return ret;} e.g GetResult ("a", ["B,c", "C,b"])//[a,b,c]//[b,a,c]//[b,c,a]//[a,c,b]//[c,a,b]//[c,b,a]static List<string > GetResult (String val, list<string> oldresult) {var ret = new list<string> (); for (var i = 0;i < Oldresult . Count; i++) {var r = GetResult (val, Oldresult[i]); foreach (var Str in R) {ret. ADD (str);}} return ret;} e.g. getresult ("A", "B,c,d")//[a,b,c,d]//[b,a,c,d]//[b,c,a,d]//[b,c,d,a]static list<string> GetResult ( String val, string old) {var newret = new list<string> (), and var arr = old. Split (', '); for (var i = 0;i <= arr. Length; i++) {var tmp = new list<string> (arr); TMP. Insert (I,val); Newret.add (String. Join (",", TMP)); return Newret;}

Algorithm exercise--C # DFS full permutation algorithm

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.