Skip to content

Commit 5ce1d7a

Browse files
committed
[23.01.11/Python] 유기농 배추
1 parent fec10a1 commit 5ce1d7a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Python_BOJ_2023/1012.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 유기농 배추
2+
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
3+
4+
def dfs(x, y):
5+
visited[x][y] = True
6+
for i in range(4):
7+
nx, ny = x + dx[i], y + dy[i]
8+
if 0 <= nx < n and 0 <= ny < m:
9+
if not visited[nx][ny] and graph[nx][ny] == 1:
10+
dfs(nx, ny)
11+
12+
for _ in range(int(input())):
13+
m, n, k = map(int, input().split())
14+
graph = [[0 for _ in range(m)] for _ in range(n)]
15+
visited = [[False for _ in range(m)] for _ in range(n)]
16+
17+
for _ in range(k):
18+
y, x = map(int, input().split())
19+
graph[x][y] = 1
20+
21+
cnt = 0
22+
for i in range(n):
23+
for j in range(m):
24+
if graph[i][j] == 1 and not visited[i][j]:
25+
dfs(i, j)
26+
cnt += 1
27+
print(cnt)

0 commit comments

Comments
 (0)