Advertisement
GalinaKG

Bombs

Sep 20th, 2022
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. from collections import deque
  2.  
  3.  
  4. def check_index(matrix, bomb_row, bomb_col):
  5.     neighbors = [
  6.         [bomb_row - 1, bomb_col - 1],
  7.         [bomb_row - 1, bomb_col],
  8.         [bomb_row - 1, bomb_col + 1],
  9.         [bomb_row, bomb_col - 1],
  10.         [bomb_row, bomb_col + 1],
  11.         [bomb_row + 1, bomb_col - 1],
  12.         [bomb_row + 1, bomb_col],
  13.         [bomb_row + 1, bomb_col + 1]
  14.     ]
  15.  
  16.     valid = []
  17.     for row, col in neighbors:
  18.         if 0 <= row < len(matrix) and 0 <= col < len(matrix) and matrix[row][col] > 0:
  19.             valid.append([row, col])
  20.     return valid
  21.  
  22.  
  23. rows = int(input())
  24. matrix = []
  25.  
  26. for _ in range(rows):
  27.     row = [int(x) for x in input().split()]
  28.     matrix.append(row)
  29.  
  30. bombs = deque(input().split())
  31.  
  32. while bombs:
  33.     bomb_coordinates = bombs.popleft()
  34.     bomb_row, bomb_col = [int(x) for x in bomb_coordinates.split(",")]
  35.     bomb_value = matrix[bomb_row][bomb_col]
  36.     if bomb_value <= 0:
  37.         continue
  38.     matrix[bomb_row][bomb_col] = 0
  39.  
  40.     neighbours = check_index(matrix, bomb_row, bomb_col)
  41.     for row, col in neighbours:
  42.         matrix[row][col] -= bomb_value
  43.  
  44. alive_cells = 0
  45. sum_cells = 0
  46. for row_i in range(rows):
  47.     for col_i in range(rows):
  48.         if matrix[row_i][col_i] > 0:
  49.             alive_cells += 1
  50.             sum_cells += matrix[row_i][col_i]
  51.  
  52. print(f"Alive cells: {alive_cells}")
  53. print(f"Sum: {sum_cells}")
  54.  
  55. for i in range(rows):
  56.     print(*matrix[i])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement