Advertisement
erikest

Recursion over substitution combinations

Feb 2nd, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Whatever
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int[,] matrix = new int[4, 4] { { 1, 0, 0, 0 }, { 0, 2, 0, 0 }, { 0, 0, 3, 0 }, { 0, 0, 0, 0 } };
  13. DoThing(matrix, 4, 4);
  14. }
  15.  
  16. static int[][] startGenerate(int[] nums, int length, out int rows)
  17. {
  18. rows=1;
  19. for (int i = 0; i < length; i++)
  20. rows *= nums[i];
  21.  
  22. int[][] answers = new int[rows][];
  23.  
  24. for (int i = 0; i < rows; i++)
  25. answers[i] = new int[length];
  26.  
  27. int index = 0;
  28.  
  29. generate(nums, length, 0, new int[length], answers,ref index);
  30.  
  31. return answers;
  32. }
  33.  
  34. static void generate(int[] a, int len, int x, int[] cur, int[][] ans,ref int index)
  35. {
  36. if (x >= len)
  37. {
  38. cur.CopyTo(ans[index++], 0);
  39. return;
  40. }
  41.  
  42. for (int i = 0; i < a[x]; i++)
  43. {
  44. cur[x] = i;
  45. generate(a, len, x + 1, cur, ans,ref index);
  46. }
  47.  
  48. cur[x] = 0;
  49. }
  50.  
  51. static void DoThing(int[,] m, int rows, int cols)
  52. {
  53. int numOfSubs=0;
  54. int[][] subs = new int[rows * cols][];
  55.  
  56. //Get all the substitution locations, the numbers to substitute and how many there are
  57. for (int i=0;i<rows;i++)
  58. for (int j = 0; j < cols; j++)
  59. {
  60. if (m[i, j] > 0)
  61. {
  62. int[] sub;
  63. int numSubsForThisEntry = GetSubs(m[i,j], out sub);
  64. int[] subRecord = new int[numSubsForThisEntry + 3];
  65. subRecord[0] = numSubsForThisEntry;
  66. subRecord[1] = i;
  67. subRecord[2] = j;
  68. sub.CopyTo(subRecord, 3);
  69. subs[numOfSubs++] = subRecord;
  70. }
  71. }
  72.  
  73. if (numOfSubs == 0) return; //Don't recurse if no substitutions
  74.  
  75. PrintArray(m, rows, cols, "Number of Subs for this array: " + numOfSubs);
  76.  
  77. //generate an array of all possible substutions
  78. int[] generateNums = new int[numOfSubs];
  79. for (int i = 0; i < numOfSubs; i++)
  80. generateNums[i] = subs[i][0];
  81. int totalCombinations;
  82. int[][] arrayOfSubstitutionIndexes = startGenerate(generateNums, numOfSubs, out totalCombinations);
  83.  
  84. //go over each set of substitutions
  85. for (int i = 0; i < totalCombinations; i++)
  86. {
  87. //with this set of substitution indexes
  88. //make a substitution at every substitution location identified previously
  89. int[] subIndexes = arrayOfSubstitutionIndexes[i];
  90. for (int j = 0; j < numOfSubs; j++)
  91. {
  92. m[subs[j][1], subs[j][2]] = subs[j][3 + subIndexes[j]];
  93. }
  94.  
  95.  
  96. //Transform
  97. //DoTransform(m, rows, cols);
  98.  
  99. //Recursive call
  100.  
  101. DoThing(m, rows, cols);
  102.  
  103. }
  104.  
  105.  
  106. }
  107.  
  108. static int GetSubs(int num, out int[] subs)
  109. {
  110. if (num == 1)
  111. subs = new int[] { 2, 3 };
  112. else if (num == 2)
  113. subs = new int[] { 3, 4 };
  114. else if (num == 3)
  115. subs = new int[] { 5 , 5 };
  116. else
  117. subs = new int[] {0 , 0};
  118.  
  119. return 2;
  120. }
  121.  
  122. static void PrintArray(int[,] m, int rows, int cols, string header)
  123. {
  124. Console.WriteLine(header);
  125. for (int i = 0; i < rows; i++)
  126. {
  127. for (int j = 0; j < cols; j++)
  128. {
  129. Console.Write(m[i, j] + "\t");
  130. }
  131. Console.WriteLine("");
  132. }
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement