Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2020
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace Bombs
  6. {
  7. class Program
  8. {
  9. public static List<int> bombsPossition = new List<int>();
  10. public static List<int> damageSpread = new List<int>() { -1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1 };
  11. public static int[,] bombsMatrix;
  12. static void Main(string[] args)
  13. {
  14. var size = int.Parse(Console.ReadLine());
  15.  
  16. bombsMatrix = new int[size, size];
  17.  
  18. Initialaize(bombsMatrix);
  19.  
  20. bombsPossition = Console.ReadLine()
  21. .Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
  22. .Select(int.Parse)
  23. .ToList();
  24.  
  25. for (int explode = 0; explode < bombsPossition.Count(); explode += 2)
  26. {
  27. var bombRow = bombsPossition[explode];
  28. var bombCol = bombsPossition[explode + 1];
  29. DamageDone(bombRow, bombCol);
  30. bombsMatrix[bombRow, bombCol] = 0;
  31.  
  32. }
  33.  
  34. var sum = 0;
  35. var cellCount = 0;
  36. for (int row = 0; row < bombsMatrix.GetLength(0); row++)
  37. {
  38. for (int col = 0; col < bombsMatrix.GetLength(1); col++)
  39. {
  40. if (bombsMatrix[row, col] > 0)
  41. {
  42. sum += bombsMatrix[row, col];
  43. cellCount++;
  44. }
  45. }
  46. }
  47. Console.WriteLine($"Alive cells: {cellCount}");
  48. Console.WriteLine($"Sum: {sum}");
  49. PrintMatrix();
  50.  
  51. }
  52.  
  53. private static void PrintMatrix()
  54. {
  55. for (int row = 0; row < bombsMatrix.GetLength(0); row++)
  56. {
  57. for (int col = 0; col < bombsMatrix.GetLength(1); col++)
  58. {
  59. Console.Write(bombsMatrix[row, col] + " ");
  60. }
  61. Console.WriteLine();
  62. }
  63. }
  64.  
  65. private static void DamageDone(int bombRow, int bombCol)
  66. {
  67. for (int dmg = 0; dmg < damageSpread.Count() - 1; dmg += 2)
  68. {
  69. var row = bombRow + damageSpread[dmg];
  70. var col = bombCol + damageSpread[dmg + 1];
  71.  
  72. if (IsInsideNotABomb(bombsMatrix, row, col) && bombsMatrix[row, col] > 0)
  73. {
  74. bombsMatrix[row, col] -= bombsMatrix[bombRow, bombCol];
  75. }
  76. ;
  77. }
  78. }
  79.  
  80. private static bool IsInsideNotABomb(int[,] bombsMatrix, int row, int col)
  81. {
  82. return row >= 0 && row < bombsMatrix.GetLength(0) &&
  83. col >= 0 && col < bombsMatrix.GetLength(1) &&
  84. !IsBomb(row, col);
  85. }
  86.  
  87. private static bool IsBomb(int row, int col)
  88. {
  89. for (int bomb = 0; bomb < bombsPossition.Count(); bomb += 2)
  90. {
  91. if (row == bombsPossition[bomb] && col == bombsPossition[bomb + 1])
  92. {
  93. return true;
  94. }
  95. }
  96.  
  97. return false;
  98. }
  99.  
  100. private static void Initialaize(int[,] bombsMatrix)
  101. {
  102. for (int row = 0; row < bombsMatrix.GetLength(0); row++)
  103. {
  104. var input = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries)
  105. .Select(int.Parse)
  106. .ToArray();
  107.  
  108. for (int col = 0; col < bombsMatrix.GetLength(1); col++)
  109. {
  110. bombsMatrix[row, col] = input[col];
  111. }
  112. }
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement