MeGaDeTH_91

09. CrossFire

Feb 1st, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _9._Crossfire
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int[] matrixDimension = Console.ReadLine()
  12. .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  13. .Select(int.Parse)
  14. .ToArray();
  15.  
  16. int rowCount = matrixDimension[0];
  17. int colCount = matrixDimension[1];
  18.  
  19. List<List<int>> matrix = new List<List<int>>();
  20.  
  21. int startValue = 1;
  22. for (int rows = 0; rows < rowCount; rows++)
  23. {
  24. List<int> rowList = new List<int>();
  25. for (int cols = 0; cols < colCount; cols++)
  26. {
  27. rowList.Add(startValue);
  28. startValue++;
  29. }
  30. matrix.Add(rowList);
  31. }
  32.  
  33. string command;
  34. while ((command = Console.ReadLine()) != "Nuke it from orbit")
  35. {
  36. int[] nukeParams = command
  37. .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  38. .Select(int.Parse)
  39. .ToArray();
  40.  
  41. int impactRow = nukeParams[0];
  42. int impactCol = nukeParams[1];
  43. int radius = nukeParams[2];
  44.  
  45. CrossFire(matrix, impactRow, impactCol, radius);
  46. ArrangeElements(matrix);
  47.  
  48. }
  49. PrintMatrix(matrix);
  50. }
  51.  
  52. private static void PrintMatrix(List<List<int>> matrix)
  53. {
  54. foreach (var m in matrix)
  55. {
  56. Console.WriteLine(string.Join(" ", m));
  57. }
  58. }
  59.  
  60. private static void ArrangeElements(List<List<int>> matrix)
  61. {
  62. for (int rows = 0; rows < matrix.Count; rows++)
  63. {
  64. matrix[rows].RemoveAll(x => x == 0);
  65. if(matrix[rows].Count == 0)
  66. {
  67. matrix.RemoveAt(rows);
  68. rows--;
  69. }
  70. }
  71. }
  72.  
  73. private static void CrossFire(List<List<int>> matrix, int impactRow, int impactCol, int radius)
  74. {
  75. for (int rows = 0; rows < matrix.Count; rows++)
  76. {
  77. for (int cols = 0; cols < matrix[rows].Count; cols++)
  78. {
  79. if ((rows == impactRow && Math.Abs(cols - impactCol) <= radius) ||
  80. (cols == impactCol && Math.Abs(rows - impactRow) <= radius))
  81. {
  82. matrix[rows][cols] = 0;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment