Skip to content

Commit 4a6b4ff

Browse files
committed
Python : 유기농배추 (dfs)
1 parent a0b580c commit 4a6b4ff

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

.idea/workspace.xml

Lines changed: 16 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BOJ_Python/1012_4.py

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

0 commit comments

Comments
 (0)