1012: 유기농 배추

ewillwin·2023년 6월 12일
0

Problem Solving (BOJ)

목록 보기
62/230

import sys
from collections import deque
sys.setrecursionlimit(10**9)

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def dfs(x, y):
	global count

	if x < 0 or y < 0 or x >= N or y >= M:
		return
	
	if board[x][y] == 1:
		count += 1
		board[x][y] = 0
		for i in range(4):
			nx = x + dx[i]; ny = y + dy[i]
			dfs(nx, ny)


T = int(input())

for _ in range(T):
	M, N, K = map(int, sys.stdin.readline().split())
	board = []
	for n in range(N):
		board.append([0 for m in range(M)])

	for __ in range(K):
		t0, t1 = map(int, sys.stdin.readline().split())
		board[t1][t0] = 1
	
	count = 0; count_list = []
	for i in range(N):
		for j in range(M):
			if board[i][j] == 1:
				dfs(i, j)
				count_list.append(count)
				count = 0
	
	print(len(count_list))
  • dfs 순회하면서 섬의 개수 구하기
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글