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