Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. using System;
  2.  
  3. namespace fillTheMatrix
  4. {
  5. class MainClass
  6. {
  7. static void Main(string[] args)
  8. {
  9. int size = int.Parse(Console.ReadLine());
  10. string point = Console.ReadLine();
  11. var sum = 1;
  12.  
  13. int[,] matrix = new int[size, size];
  14. // first part
  15. /*
  16. for (int col = 0; col < size; col++)
  17. {
  18. for (int row = 0; row < size; row++)
  19. {
  20. matrix[row, col] = sum;
  21. sum++;
  22. }
  23. }
  24. */
  25.  
  26. //second part
  27. /*
  28. for (int col = 0; col < size; col++)
  29. {
  30. int checker = col % 2;
  31. if (checker == 0)
  32. {
  33. for (int row = 0; row < size; row++)
  34. {
  35. matrix[row, col] = sum;
  36. sum++;
  37. }
  38. }
  39. else
  40. {
  41. for (int row = size - 1; row >= 0; row--)
  42. {
  43. matrix[row, col] = sum;
  44. sum++;
  45.  
  46. }
  47. }
  48. }
  49. */
  50. //third part
  51. /*
  52. for (int i = size - 1; i >= 0; i--)
  53. {
  54. int row = i;
  55. int col = 0;
  56.  
  57. while (true)
  58. {
  59. matrix[row, col] = sum;
  60. sum++;
  61. if (row == size - 1)
  62. {
  63. break;
  64. }
  65. else
  66. {
  67. row++;
  68. col++;
  69. }
  70. }
  71. }
  72.  
  73. for (int i = 1; i < size; i++)
  74. {
  75. int row = i;
  76. int col = 0;
  77.  
  78. while (true)
  79. {
  80. matrix[col, row] = sum;
  81. sum++;
  82. if (row == size - 1)
  83. {
  84. break;
  85. }
  86. else
  87. {
  88. row++;
  89. col++;
  90. }
  91. }
  92. }
  93. */
  94.  
  95. string direction = "down";
  96. int row = 0;
  97. int col = 0;
  98. for (int i = 0; i < size * size; i++)
  99. {
  100. if (direction == "down" && matrix[row, col] == 0)
  101. {
  102. matrix[row, col] = sum;
  103. if (row == size - 1)
  104. {
  105. direction = "right";
  106. }
  107. }
  108. if (direction == "right")
  109. {
  110. matrix[row, col] = sum;
  111. if (col == size-1)
  112. {
  113. direction = "up";
  114. }
  115.  
  116. }
  117. if (direction =="up")
  118. {
  119. matrix[row, col] = sum;
  120. if (row <= 0)
  121. {
  122. direction = "left";
  123. }
  124. }
  125. if (direction =="left" && matrix[row,col] == 0)
  126. {
  127. matrix[row, col] = sum;
  128. if (col <= 0)
  129. {
  130. direction = "down";
  131. }
  132.  
  133. }
  134.  
  135. sum++;
  136. switch (direction)
  137. {
  138. case "down":
  139. row++;
  140. break;
  141. case "right":
  142. col++;
  143. break;
  144. case "up":
  145. row--;
  146. break;
  147. case "left":
  148. col--;
  149. break;
  150.  
  151. default:
  152. break;
  153. }
  154.  
  155. Print(matrix);
  156. }
  157. }
  158.  
  159. static void Print(int[,] matrixToPrint)
  160. {
  161. for (int row = 0; row < matrixToPrint.GetLength(0); row++)
  162. {
  163. for (int col = 0; col < matrixToPrint.GetLength(1); col++)
  164. {
  165. Console.Write(matrixToPrint[row, col] + " ");
  166. }
  167. Console.WriteLine();
  168. }
  169. }
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement