-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11123.py
More file actions
41 lines (30 loc) · 936 Bytes
/
11123.py
File metadata and controls
41 lines (30 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from collections import deque
import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
H, W = map(int, input().split())
board = [list(input()) for _ in range(H)]
ans = 0
dy = (1, 0, -1, 0)
dx = (0, 1, 0, -1)
def is_valid_coord(y, x):
return 0 <= y < W and 0 <= x < H
def bfs(y, x):
dq = deque()
dq.append((y, x))
while dq:
y, x = dq.popleft()
for k in range(4):
ny = y + dy[k]
nx = x + dx[k]
if is_valid_coord(ny, nx) and board[nx][ny] == '#':
dq.append((ny, nx))
board[nx][ny] = '.'
for i in range(H):
for j in range(W):
if board[i][j] == '#':
board[i][j] = '.'
bfs(j, i) # y, x
ans += 1
print(ans)