Advertisement
osman1997

8. bombs

Feb 16th, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Bombs
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int n = int.Parse(Console.ReadLine());
  12.  
  13. int[,] matrix = new int[n, n];
  14.  
  15. for (int row = 0; row < n; row++)
  16. {
  17. int[] data = Console.ReadLine().Split().Select(int.Parse).ToArray();
  18.  
  19. for (int col = 0; col < n; col++)
  20. {
  21. matrix[row, col] = data[col];
  22. }
  23. }
  24.  
  25. string[] command = Console.ReadLine().Split();
  26.  
  27. string[] first = command[0].Split(",");
  28. int firsBombRow = int.Parse(first[0]);
  29. int firstBombCol = int.Parse(first[1]);
  30.  
  31. string[] second = command[1].Split(",");
  32. int secondBombRow = int.Parse(second[0]);
  33. int secondBombCol = int.Parse(second[1]);
  34.  
  35. string[] third = command[2].Split(",");
  36. int thirdBombRow = int.Parse(third[0]);
  37. int thirdBombCol = int.Parse(third[1]);
  38.  
  39. List<int[]> bombs = new List<int[]>();
  40. bombs.Add(new int[] { firsBombRow, firstBombCol });
  41. bombs.Add(new int[] { secondBombRow, secondBombCol });
  42. bombs.Add(new int[] { thirdBombRow, thirdBombCol });
  43.  
  44. for (int i = 0; i < bombs.Count; i++)
  45. {
  46. int rowBomb = bombs[i][0];
  47. int colBomb = bombs[i][1];
  48.  
  49. if (IsValidCellCell(rowBomb - 1, colBomb - 1, n) && matrix[rowBomb - 1, colBomb - 1] >= 0)
  50. {
  51. matrix[rowBomb - 1, colBomb - 1] -= matrix[rowBomb, colBomb];
  52. }
  53. if (IsValidCellCell(rowBomb - 1, colBomb + 1, n) && matrix[rowBomb - 1, colBomb + 1] >= 0)
  54. {
  55. matrix[rowBomb - 1, colBomb + 1] -= matrix[rowBomb, colBomb];
  56. }
  57. if (IsValidCellCell(rowBomb - 1, colBomb, n) && matrix[rowBomb - 1, colBomb] >= 0)
  58. {
  59. matrix[rowBomb - 1, colBomb] -= matrix[rowBomb, colBomb];
  60. }
  61.  
  62. if (IsValidCellCell(rowBomb + 1, colBomb - 1, n) && matrix[rowBomb + 1, colBomb - 1] >= 0)
  63. {
  64. matrix[rowBomb + 1, colBomb - 1] -= matrix[rowBomb, colBomb];
  65. }
  66. if (IsValidCellCell(rowBomb + 1, colBomb + 1, n) && matrix[rowBomb + 1, colBomb + 1] >= 0)
  67. {
  68. matrix[rowBomb + 1, colBomb + 1] -= matrix[rowBomb, colBomb];
  69. }
  70. if (IsValidCellCell(rowBomb + 1, colBomb, n) && matrix[rowBomb + 1, colBomb] >= 0)
  71. {
  72. matrix[rowBomb + 1, colBomb] -= matrix[rowBomb, colBomb];
  73. }
  74. if (IsValidCellCell(rowBomb, colBomb - 1, n) && matrix[rowBomb, colBomb - 1] >= 0)
  75. {
  76. matrix[rowBomb, colBomb - 1] -= matrix[rowBomb, colBomb];
  77. }
  78. if (IsValidCellCell(rowBomb, colBomb + 1, n) && matrix[rowBomb, colBomb + 1] >= 0)
  79. {
  80. matrix[rowBomb, colBomb + 1] -= matrix[rowBomb, colBomb];
  81. }
  82. }
  83.  
  84. matrix[firsBombRow, firstBombCol] = 0;
  85. matrix[secondBombRow, secondBombCol] = 0;
  86. matrix[thirdBombRow, thirdBombCol] = 0;
  87.  
  88. int aliveCells = 0;
  89. int sum = 0;
  90.  
  91. for (int row = 0; row < n; row++)
  92. {
  93. for (int col = 0; col < n; col++)
  94. {
  95. if (matrix[row, col] > 0)
  96. {
  97. aliveCells++;
  98. sum += matrix[row, col];
  99. }
  100. }
  101. }
  102. Console.WriteLine($"Alive cells: {aliveCells}");
  103. Console.WriteLine($"Sum: {sum}");
  104.  
  105. for (int row = 0; row < n; row++)
  106. {
  107. for (int col = 0; col < n; col++)
  108. {
  109. if (col != n)
  110. {
  111. Console.Write($"{matrix[row, col]} ");
  112. }
  113. else
  114. {
  115. Console.Write($"{matrix[row, col]}");
  116. }
  117. }
  118. Console.WriteLine();
  119. }
  120. }
  121. static bool IsValidCellCell(int row, int col, int n)
  122. {
  123. return row >= 0 && row < n && col >= 0 && col < n;
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement