MeGaDeTH_91

06.

Feb 1st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _6._Target_Practice
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int[] matrixSizes = Console.ReadLine()
  12. .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  13. .Select(int.Parse)
  14. .ToArray();
  15.  
  16. string snake = Console.ReadLine();
  17.  
  18. int rowCount = matrixSizes[0];
  19. int colCount = matrixSizes[1];
  20.  
  21. string[,] matrix = new string[rowCount, colCount];
  22.  
  23. FillMatrix(matrix, snake, rowCount, colCount);
  24.  
  25. int[] shotParams = Console.ReadLine()
  26. .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  27. .Select(int.Parse)
  28. .ToArray();
  29.  
  30. int impactRow = shotParams[0];
  31. int impactCol = shotParams[1];
  32. int radius = shotParams[2];
  33.  
  34. FireAShot(matrix, impactRow, impactCol, radius, rowCount, colCount);
  35.  
  36. DropCharacters(matrix, rowCount, colCount);
  37.  
  38. PrintMatrix(matrix, rowCount, colCount);
  39. }
  40.  
  41. private static void DropCharacters(string[,] matrix, int rowCount, int colCount)
  42. {
  43. for (int rows = rowCount - 1; rows > 0; rows--)
  44. {
  45. int rowMarker = rowCount - 1;
  46.  
  47. while (rowMarker > 0)
  48. {
  49. if (matrix[rowMarker, rows] == " ")
  50. {
  51. if (matrix[rowMarker - 1, rows] != " ")
  52. {
  53. matrix[rowMarker, rows] = matrix[rowMarker - 1, rows];
  54. matrix[rowMarker - 1, rows] = " ";
  55. rowMarker++;
  56. }
  57. else
  58. {
  59. for (int index = rowMarker; index >= 0; index--)
  60. {
  61. if (matrix[index, rows] != " ")
  62. {
  63. matrix[rowMarker, rows] = matrix[index, rows];
  64. matrix[index, rows] = " ";
  65. }
  66. }
  67. }
  68. }
  69. rowMarker--;
  70. }
  71. }
  72. }
  73.  
  74. private static void PrintMatrix(string[,] matrix, int rowCount, int colCount)
  75. {
  76. for (int rows = 0; rows < rowCount; rows++)
  77. {
  78.  
  79. for (int cols = 0; cols < colCount; cols++)
  80. {
  81. Console.Write($"{matrix[rows, cols]}");
  82. }
  83. Console.WriteLine();
  84. }
  85. }
  86.  
  87. private static void FireAShot(string[,] matrix, int impactRow, int impactCol, int radius, int rowCount, int colCount)
  88. {
  89. int rowUpDiff = impactRow - radius;
  90. int rowDownDiff = impactRow + radius;
  91.  
  92. int colRightDiff = impactCol + radius;
  93. int colLeftDiff = impactCol - radius;
  94.  
  95. int rightStep = Math.Min(colRightDiff + 1, colCount);
  96. int downStep = Math.Min(rowDownDiff + 1, rowCount);
  97. int leftStep = Math.Max(colLeftDiff, 0);
  98. int upStep = Math.Max(rowUpDiff, 0);
  99.  
  100. int counter = 0;
  101. for (int right = impactCol; right < rightStep; right++)
  102. {
  103. matrix[impactRow, right] = " ";
  104. if (counter == 1)
  105. {
  106. if (impactRow + 1 < rowCount)
  107. {
  108. matrix[impactRow + 1, right] = " ";
  109. }
  110. if (impactRow - 1 >= 0)
  111. {
  112. matrix[impactRow - 1, right] = " ";
  113. }
  114. }
  115. counter++;
  116. }
  117. for (int down = impactRow; down < downStep; down++)
  118. {
  119. matrix[down, impactCol] = " ";
  120. }
  121. counter = 0;
  122. for (int left = impactCol; left >= leftStep; left--)
  123. {
  124. matrix[impactRow, left] = " ";
  125. if (counter == 1)
  126. {
  127. if (impactRow + 1 < rowCount)
  128. {
  129. matrix[impactRow + 1, left] = " ";
  130. }
  131. if (impactRow - 1 >= 0)
  132. {
  133. matrix[impactRow - 1, left] = " ";
  134. }
  135. }
  136. counter++;
  137. }
  138. for (int up = impactRow; up >= upStep; up--)
  139. {
  140. matrix[up, impactCol] = " ";
  141. }
  142. }
  143.  
  144. private static void FillMatrix(string[,] matrix, string snakeInput, int rowCount, int colCount)
  145. {
  146. Queue<string> snake = new Queue<string>();
  147.  
  148. for (int i = 0; i < snakeInput.Length; i++)
  149. {
  150. snake.Enqueue(snakeInput[i].ToString());
  151. }
  152.  
  153. int rowCounter = 1;
  154. for (int rows = rowCount - 1; rows >= 0; rows--)
  155. {
  156. int currCols = 0;
  157. if (rowCounter % 2 != 0)
  158. {
  159. currCols = colCount - 1;
  160. }
  161. else
  162. {
  163. currCols = 0;
  164. }
  165.  
  166. while (currCols >= 0 && currCols < colCount)
  167. {
  168. string currLetter = snake.Dequeue();
  169.  
  170. matrix[rows, currCols] = currLetter;
  171.  
  172. snake.Enqueue(currLetter);
  173. if (rowCounter % 2 != 0)
  174. {
  175. currCols--;
  176. }
  177. else
  178. {
  179. currCols++;
  180. }
  181. }
  182. rowCounter++;
  183. }
  184. }
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment