컴공생의 다이어리

[파이썬, Python] 백준 1012번 : 유기농 배추 본문

Development/Algorithm & Coding Test

[파이썬, Python] 백준 1012번 : 유기농 배추

컴공 K 2022. 5. 1. 00:01

백준 1012번 : 유기농 배추

(문제 바로가기)

 

 

 

내 코드

import sys
sys.setrecursionlimit(10**6)

def dfs(x, y):
    visited[x][y] = True
    directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

    for dx, dy in directions:
        nx, ny = x + dx, y + dy
        if nx < 0 or nx >= n or ny < 0 or ny >= m:
            continue
        if array[nx][ny] and not visited[nx][ny]:
            dfs(nx, ny)

for _ in range(int(input())):
    m, n, k = map(int, input().split())

    array = [[0] * m for _ in range(n)]
    visited = [[False] * m for _ in range(n)]

    for _ in range(k):
        y, x = map(int, input().split())
        array[x][y] = 1

    result = 0
    for i in range(n):
        for j in range(m):
            if array[i][j] and not visited[i][j]:
                dfs(i, j)
                result += 1

    print(result)

 

 

728x90
Comments