Advertisement
Guest User

Untitled

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