-
Notifications
You must be signed in to change notification settings - Fork 0
/
유기농 배추.py
48 lines (38 loc) · 1.16 KB
/
유기농 배추.py
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
import sys
sys.setrecursionlimit(10**6)
readline = lambda: sys.stdin.readline().strip()
T = int(readline())
diff = [(-1, 0), (0, -1), (1, 0), (0, 1)]
def dfs(x, y):
visited[x][y] = True
for dx, dy in diff:
nx = x + dx
ny = y + dy
if 0 <= nx < M and 0 <= ny < N:
if graph[nx][ny] and not visited[nx][ny]:
visited[nx][ny] = True
dfs(nx, ny)
def print_graph():
for line in graph:
print(line)
for _ in range(T):
M, N, K = map(int, readline().split())
graph = [[0] * N for _ in range(M)] # matrix
visited = [[False] * N for _ in range(M)]
for _ in range(K):
x, y = map(int, readline().split())
graph[x][y] = 1
all_visited = False
round = 0
while not all_visited:
all_visited = True
for y in range(N):
for x in range(M):
if graph[x][y] and not visited[x][y]:
all_visited = False
dfs(x, y)
round += 1
break
if not all_visited:
break
print(round)