Advertisement
Guest User

CrossFire

a guest
Sep 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Numerics;
  4.  
  5. namespace _09.Crossfire
  6. {
  7. class StartUp
  8. {
  9. static int[][] matrix;
  10. static void Main(string[] args)
  11. {
  12. var dimensions = Console.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)
  13. .Select(int.Parse)
  14. .ToArray();
  15.  
  16. matrix = ReadAndFillMatrix(dimensions);
  17.  
  18. var input = Console.ReadLine();
  19.  
  20. while (input != "Nuke it from orbit")
  21. {
  22. var commandTokens = input.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)
  23. .Select(int.Parse)
  24. .ToArray();
  25.  
  26. DestroyCells(commandTokens, dimensions);
  27. input = Console.ReadLine();
  28. }
  29.  
  30. PrintResult();
  31. }
  32.  
  33. private static void RemoveEmptyCells()
  34. {
  35. for (int i = 0; i < matrix.Length; i++)
  36. {
  37. // Remove destroyed cells if there is ones
  38. for (int j = 0; j < matrix[i].Length; j++)
  39. {
  40. if (matrix[i][j] < 0)
  41. {
  42. matrix[i] = matrix[i].Where(n => n > 0).ToArray();
  43. break;
  44. }
  45. }
  46.  
  47. // Remove empty rows
  48. if (matrix[i].Count() < 1)
  49. {
  50. matrix = matrix.Take(i).Concat(matrix.Skip(i + 1)).ToArray();
  51. i--;
  52. }
  53. }
  54. }
  55.  
  56. private static void PrintResult()
  57. {
  58.  
  59. for (int i = 0; i < matrix.GetLength(0); i++)
  60. {
  61. if (matrix[i].Count() > 0)
  62. {
  63. Console.WriteLine(string.Join(' ', matrix[i]));
  64. }
  65. }
  66. }
  67.  
  68. private static void DestroyCells(int[] commandTokens, int[] dimensions)
  69. {
  70. var hitRow = commandTokens[0];
  71. var hitCol = commandTokens[1];
  72. var hitWave = commandTokens[2];
  73.  
  74. // Mark destroyed part of the column
  75. for (int row = hitRow - hitWave; row <= hitRow + hitWave; row++)
  76. {
  77. if (IsInMatrix(row, hitCol, matrix))
  78. {
  79. matrix[row][hitCol] = -1;
  80. }
  81. }
  82.  
  83. // Mark destroyed part of the row
  84. for (int col = hitCol - hitWave; col <= hitCol + hitWave; col++)
  85. {
  86. if (IsInMatrix(hitRow, col, matrix))
  87. {
  88. matrix[hitRow][col] = -1;
  89. }
  90. }
  91. RemoveEmptyCells();
  92. }
  93. private static bool IsInMatrix(int row, int col, int[][] matrix)
  94. {
  95. return row >= 0 && col >= 0 && row < matrix.Length && col < matrix[row].Length;
  96. }
  97.  
  98.  
  99. private static int[][] ReadAndFillMatrix(int[] dimensions)
  100. {
  101. var matrix = new int[dimensions[0]][];
  102. int count = 1;
  103.  
  104. for (int i = 0; i < dimensions[0]; i++)
  105. {
  106. matrix[i] = new int[dimensions[1]];
  107. for (int j = 0; j < dimensions[1]; j++)
  108. {
  109. matrix[i][j] = count;
  110. count++;
  111. }
  112. }
  113. return matrix;
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement