Advertisement
Guest User

Untitled

a guest
Jan 29th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Crossfire
  8. {
  9. class Crossfire
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<List<int>> matrix = GetMatrix();
  14. string input = "";
  15.  
  16. while ((input = Console.ReadLine()) != "Nuke it from orbit")
  17. {
  18. int[] targetCoordinates = input
  19. .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  20. .Select(int.Parse).ToArray();
  21. matrix = DestroyMatrixCells(matrix, targetCoordinates);
  22. }
  23. PrintMatrix(matrix);
  24. }
  25.  
  26. private static List<List<int>> DestroyMatrixCells(List<List<int>> matrix, int[] coordinates)
  27. {
  28. int targetRow = coordinates[0];
  29. int targetCol = coordinates[1];
  30. int radius = coordinates[2];
  31. bool containsDestroyedCells = false;
  32. // update horizontal cells
  33. if (targetRow >= 0 && targetRow < matrix.Count)
  34. {
  35. for (int col = Math.Max(0, targetCol - radius); col <= Math.Min(targetCol + radius, matrix[targetRow].Count - 1); col++)
  36. {
  37. matrix[targetRow][col] = 0;
  38. containsDestroyedCells = true;
  39. }
  40. }
  41. // update vertical cells
  42. if (targetCol >= 0)
  43. {
  44. for (int row = Math.Max(0, targetRow - radius); row <= Math.Min(targetRow + radius, matrix.Count - 1); row++)
  45. {
  46. if (targetCol < matrix[row].Count)
  47. {
  48. matrix[row][targetCol] = 0;
  49. containsDestroyedCells = true;
  50. }
  51. }
  52. }
  53. // remove destroyed matrix cells
  54. if (containsDestroyedCells)
  55. {
  56. for (int row = 0; row < matrix.Count; row++)
  57. {
  58. if (matrix[row].Contains(0))
  59. {
  60. List<int> elements = new List<int>();
  61. for (int col = 0; col < matrix[row].Count; col++)
  62. if (matrix[row][col] != 0)
  63. elements.Add(matrix[row][col]);
  64. if (elements.Count > 0)
  65. matrix[row] = elements;
  66. else
  67. {
  68. matrix.RemoveAt(row);
  69. row--;
  70. }
  71. }
  72. }
  73. }
  74. return matrix;
  75. }
  76.  
  77. private static void PrintMatrix(List<List<int>> matrix)
  78. {
  79. for (int row = 0; row < matrix.Count; row++)
  80. Console.WriteLine(string.Join(" ", matrix[row]));
  81. }
  82.  
  83. private static List<List<int>> GetMatrix()
  84. {
  85. int[] matrixDimensions = Console.ReadLine()
  86. .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  87. .Select(int.Parse).ToArray();
  88. int rows = matrixDimensions[0];
  89. int cols = matrixDimensions[1];
  90. List<List<int>> matrix = new List<List<int>>();
  91. int number = 1;
  92. for (int row = 0; row < rows; row++)
  93. {
  94. matrix.Add(new List<int>());
  95. for (int col = 0; col < cols; col++)
  96. matrix[row].Add(number++);
  97. }
  98. return matrix;
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement