braveheart1989

Matrix Generator

Dec 15th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace E7.Matrix_Generator
  2. {
  3. class MatrixGenerator
  4. {
  5. static void Main(string[] args)
  6. {
  7. string[] input = Console.ReadLine().Split();
  8. int rows = int.Parse(input[1]);
  9. int cols = int.Parse(input[2]);
  10.  
  11. switch (input[0])
  12. {
  13. case "A": TypeA(rows, cols); break;
  14. case "B": TypeB(rows, cols); break;
  15. case "C": TypeC(rows, cols); break;
  16. case "D": TypeD(rows, cols); break;
  17. default: break;
  18. }
  19. }
  20. // Метод за тип А
  21. static void TypeA(int rows, int cols)
  22. {
  23. int[,] matrix = new int[rows, cols];
  24. int value = 1;
  25. // Два вложени цикъла, които пълнят матрицата със стойности
  26. for (int i = 0; i < cols; i++)
  27. {
  28. for (int j = 0; j < rows; j++)
  29. {
  30. matrix[j, i] = value;
  31. value++;
  32. }
  33. }
  34. // Два вложени цикъла, които изчертават матрицата
  35. for (int i = 0; i < rows; i++)
  36. {
  37. for (int j = 0; j < cols; j++)
  38. {
  39. Console.Write(matrix[i, j] + " ");
  40. }
  41. Console.WriteLine();
  42. }
  43. }
  44. // Тип B
  45. static void TypeB(int rows, int cols)
  46. {
  47. int[,] matrix = new int[rows, cols];
  48. int value = 1;
  49.  
  50. for (int i = 0; i < cols; i++)
  51. {
  52. for (int j = 0; j < rows; j++)
  53. {
  54. if (i % 2 == 0)
  55. {
  56. matrix[j, i] = value;
  57. value++;
  58. }
  59. else
  60. {
  61. matrix[rows - 1 - j, i] = value;
  62. value++;
  63. }
  64. }
  65. }
  66. for (int i = 0; i < rows; i++)
  67. {
  68. for (int j = 0; j < cols; j++)
  69. {
  70. Console.Write(matrix[i, j] + " ");
  71. }
  72. Console.WriteLine();
  73. }
  74. }
  75. //Тип C
  76. static void TypeC(int rows, int cols)
  77. {
  78. int[,] matrix = new int[rows, cols];
  79. int value = 1;
  80.  
  81. for (int i = rows - 1; i >= 0; i--)
  82. {
  83. int startR = i;
  84. for (int j = 0; j < rows - startR; j++)
  85. {
  86. if (j > cols - 1)
  87. {
  88. break;
  89. }
  90. matrix[startR + j, j] = value;
  91. value++;
  92. }
  93. }
  94.  
  95. for (int i = 1; i < cols; i++)
  96. {
  97. int startC = i;
  98. for (int j = 0; j < cols - startC; j++)
  99. {
  100. if (j > rows - 1)
  101. {
  102. break;
  103. }
  104. matrix[j, startC + j] = value;
  105. value++;
  106. }
  107. }
  108. for (int i = 0; i < rows; i++)
  109. {
  110. for (int j = 0; j < cols; j++)
  111. {
  112. Console.Write(matrix[i, j] + " ");
  113. }
  114. Console.WriteLine();
  115. }
  116. }
  117. // Тип D
  118. static void TypeD(int rows, int cols)
  119. {
  120. int[,] matrix = new int[rows, cols];
  121. int value = 1;
  122. int indexR = 0;
  123. int indexC = 0;
  124.  
  125. while (value <= rows * cols)
  126. {
  127. matrix[indexR, indexC] = value;
  128. value++; ;
  129.  
  130. bool canGoDown = (indexR + 1) < rows && matrix[indexR + 1, indexC] == 0 && !((indexC - 1) >= 0 && matrix[indexR, indexC - 1] == 0);
  131. if (canGoDown)
  132. {
  133. indexR++; continue;
  134. }
  135. bool canGoRight = (indexC + 1) < cols && matrix[indexR, indexC + 1] == 0;
  136. if (canGoRight)
  137. {
  138. indexC++; continue;
  139. }
  140. bool canGoUp = (indexR - 1) >= 0 && matrix[indexR - 1, indexC] == 0;
  141. if (canGoUp)
  142. {
  143. indexR--; continue;
  144. }
  145. bool canGoLeft = (indexC - 1) >= 0 && matrix[indexR, indexC - 1] == 0;
  146. if (canGoLeft)
  147. {
  148. indexC--; continue;
  149. }
  150. }
  151. for (int i = 0; i < rows; i++)
  152. {
  153. for (int j = 0; j < cols; j++)
  154. {
  155. Console.Write(matrix[i, j] + " ");
  156. }
  157. Console.WriteLine();
  158. }
  159. }
  160. }
  161. }
Add Comment
Please, Sign In to add comment