# bombs

Jan 23rd, 2022 (edited)
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def neighbour_cells(row_, col_, n_):
2.     valid_cells = []
3.     if row_ -1 in range(n_) and col_ - 1 in range(n_):
4.         valid_cells.append((row_-1, col_-1))
5.     if row_ -1 in range(n_) and col_ in range(n_):
6.         valid_cells.append((row_-1, col_))
7.     if row_ -1 in range(n_) and col_ + 1 in range(n_):
8.         valid_cells.append((row_-1, col_+1))
9.     if row_ in range(n_) and col_ - 1 in range(n_):
10.         valid_cells.append((row_, col_-1))
11.     if row_ in range(n_) and col_ + 1 in range(n_):
12.         valid_cells.append((row_, col_ +1))
13.     if row_ +1 in range(n_) and col_ - 1 in range(n_):
14.         valid_cells.append((row_+1, col_-1))
15.     if row_  +1 in range(n_) and col_  in range(n_):
16.         valid_cells.append((row_ +1, col_))
17.     if row_ +1 in range(n_) and col_ + 1 in range(n_):
18.         valid_cells.append((row_ +1, col_ +1))
19.     return valid_cells
20.
21. n = int(input())
22.
23. matrix = []
24. for _ in range(n):
25.     current_row = [int(n) for n in input().split()]
26.     matrix.append(current_row)
27.
28. bombs = input().split()
29. for bomb in bombs:
30.     row, col = [int(n) for n in bomb.split(',')]
31.     current_bomb = matrix[row][col]
32.     if current_bomb > 0:
33.         neighbours = neighbour_cells(row, col, n)
34.         for r, c in neighbours:
35.             if matrix[int(r)][int(c)] > 0:
36.                 matrix[int(r)][int(c)] -= current_bomb
37.
38.         matrix[row][col] = 0
39.
40. sum_alive = 0
41. number_alive = 0
42. for row in range(n):
43.     for col in range(n):
44.         if matrix[row][col] > 0:
45.             sum_alive += matrix[row][col]
46.             number_alive += 1
47.
48. print(f"Alive cells: {number_alive}")
49. print(f"Sum: {sum_alive}")
50.
51. for r in range(n):
52.     print(*matrix[r])
53.
RAW Paste Data Copied