-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy path[15]_2.py
More file actions
32 lines (25 loc) · 778 Bytes
/
[15]_2.py
File metadata and controls
32 lines (25 loc) · 778 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
import sys
sys.setrecursionlimit(100000)
def dfs(x, y):
visited[x][y] = True
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for dx, dy in directions:
nx, ny = x + dx, y + dy
if nx < 0 or nx >= n or ny < 0 or ny >= m:
continue
if array[nx][ny] and not visited[nx][ny]:
dfs(nx, ny)
for _ in range(int(input())):
m, n, k = map(int, input().split())
array = [[0] * m for _ in range(n)]
visited = [[False] * m for _ in range(n)]
for _ in range(k):
y, x = map(int, input().split())
array[x][y] = 1
result = 0
for i in range(n):
for j in range(m):
if array[i][j] and not visited[i][j]:
dfs(i, j)
result += 1
print(result)