문제
https://www.acmicpc.net/problem/1012
코드
import sys
sys.setrecursionlimit(10000)
t = int(input())
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def dfs(r, c):
visited[r][c] = 1
for i in range(4):
nx = r + dx[i]
ny = c + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if visited[nx][ny] == 0 and graph[nx][ny] == 1:
dfs(nx, ny)
for _ in range(t):
m, n, k = map(int, input().split())
graph = [[0] * m for _ in range(n)]
visited = [[0] * m for _ in range(n)]
for _ in range(k):
a, b = map(int, input().split())
graph[b][a] = 1
count = 0
for i in range(n):
for j in range(m):
if visited[i][j] == 0 and graph[i][j] == 1:
count += 1
dfs(i, j)
print(count)
'문제풀이 > DFS & BFS' 카테고리의 다른 글
[파이썬] [BFS] 백준 27211 도넛 행성 (0) | 2023.01.17 |
---|---|
@[파이썬] [DFS] 백준 2468 안전 영역 (0) | 2022.12.29 |
@[파이썬] [BFS] 백준 2178 미로 탐색 (0) | 2022.12.26 |
[파이썬] [DFS] 백준 1759 암호 만들기 (0) | 2022.05.27 |
[PyPy] [BFS] 백준 9019 DSLR (0) | 2022.05.26 |