-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ1012.py
More file actions
33 lines (26 loc) · 824 Bytes
/
BJ1012.py
File metadata and controls
33 lines (26 loc) · 824 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
import sys
sys.setrecursionlimit(10 ** 6)
def cluster(y, x):
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
data[y][x] = 0
for i in range(4):
new_x = x + dx[i]
new_y = y + dy[i]
if 0 <= new_y < c and 0 <= new_x < r and data[new_y][new_x] == 1:
cluster(new_y, new_x)
if __name__ == "__main__":
t = int(sys.stdin.readline())
for _ in range(t):
r, c, n = map(int, sys.stdin.readline().split())
data = [[0 for _ in range(r)] for _ in range(c)]
for _ in range(n):
j, i = map(int, sys.stdin.readline().split())
data[i][j] = 1
count = 0
for i in range(c):
for j in range(r):
if data[i][j] == 1:
cluster(i, j)
count += 1
print(count)