-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ1012.py
More file actions
55 lines (42 loc) · 1.33 KB
/
BJ1012.py
File metadata and controls
55 lines (42 loc) · 1.33 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
import sys
from collections import deque
TC = int(sys.stdin.readline())
for _ in range(TC):
count = 0
que = deque([])
MNK = sys.stdin.readline().split(' ')
M = int(MNK[0])
N = int(MNK[1])
K = int(MNK[2])
matrix = [[0 for __ in range(M)] for _ in range(N)]
visited = [[0 for __ in range(M)] for _ in range(N)]
for i in range(K):
ind = sys.stdin.readline().split(' ')
matrix[int(ind[1])][int(ind[0])] = 1
for k in range(N):
for q in range(M):
if matrix[k][q] == 1 and visited[k][q] != 1:
que.append([k, q])
while que:
current = que.pop()
x = current[0]
y = current[1]
visited[x][y] = 1
if x < N - 1:
if matrix[x + 1][y] == 1 and visited[x + 1][y] != 1:
que.append([x + 1, y])
visited[x + 1][y] = 1
if x > 0:
if matrix[x - 1][y] == 1 and visited[x - 1][y] != 1:
que.append([x - 1, y])
visited[x - 1][y] = 1
if y > 0:
if matrix[x][y - 1] == 1 and visited[x][y - 1] != 1:
que.append([x, y - 1])
visited[x][y - 1] = 1
if y < M - 1:
if matrix[x][y + 1] == 1 and visited[x][y + 1] != 1:
que.append([x, y + 1])
visited[x][y + 1] = 1
count += 1
print(count)