Advertisement
Guest User

Untitled

a guest
Aug 24th, 2020
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. int fieldSize = int.Parse(Console.ReadLine());
  10. int[,] field = new int[fieldSize, fieldSize];
  11. ReadFieldFromConsole(field);
  12. string[] coordinatesValues = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
  13. ExplodeTheBombs(field, coordinatesValues);
  14. int aliveCells = 0;
  15. int sumAliveCells = 0;
  16. foreach (int cell in field)
  17. {
  18. if (cell > 0)
  19. {
  20. aliveCells++;
  21. sumAliveCells += cell;
  22. }
  23. }
  24. Console.WriteLine($"Alive cells: {aliveCells}");
  25. Console.WriteLine($"Sum: {sumAliveCells}");
  26. PrintField(field);
  27. }
  28.  
  29. private static void PrintField(int[,] field)
  30. {
  31. for (int row = 0; row < field.GetLength(0); row++)
  32. {
  33. for (int col = 0; col < field.GetLength(1); col++)
  34. {
  35. Console.Write(field[row, col] + " ");
  36.  
  37. }
  38. Console.WriteLine();
  39. }
  40. }
  41.  
  42. private static void ExplodeTheBombs(int[,] field, string[] coordinatesValues)
  43. {
  44. foreach (string rowColPair in coordinatesValues)
  45. {
  46. int[] currentBombCoordinates = rowColPair
  47. .Split(",", StringSplitOptions.RemoveEmptyEntries)
  48. .Select(int.Parse)
  49. .ToArray();
  50. int currentBombRow = currentBombCoordinates[0];
  51. int currentBombCol = currentBombCoordinates[1];
  52. int currentBomb = field[currentBombRow, currentBombCol];
  53.  
  54.  
  55. for (int row = currentBombRow - 1; row <= currentBombRow + 1; row++)
  56. {
  57. for (int col = currentBombCol - 1; col <= currentBombCol + 1; col++)
  58. {
  59. if (row >= 0 && row < field.GetLength(0) && col >= 0 && col < field.GetLength(1))
  60. {
  61. if (field[row, col] <= 0 || currentBomb < 0)
  62. {
  63. continue;
  64. }
  65. field[row, col] -= currentBomb;
  66. }
  67.  
  68. }
  69.  
  70. }
  71.  
  72. }
  73. }
  74.  
  75. private static void ReadFieldFromConsole(int[,] field)
  76. {
  77. for (int row = 0; row < field.GetLength(0); row++)
  78. {
  79. int[] currentRow = Console.ReadLine()
  80. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  81. .Select(int.Parse)
  82. .ToArray();
  83. for (int col = 0; col < field.GetLength(1); col++)
  84. {
  85. field[row, col] = currentRow[col];
  86. }
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement