카테고리 없음

[백준/Python] 11123번 : 양 한마리... 양 두마리...

Jinn_12609 2025. 3. 2. 17:48

문제

 

https://www.acmicpc.net/problem/11123https://www.acmicpc.net/problem/17142


Algorithm

 

 


 

 

Code

import sys
sys.setrecursionlimit(100000)

T = int(input())

def dfs(y,x):
    graph[y][x] = '.'
    dy = [0,1,0,-1]
    dx = [1,0,-1,0]
    for k in range(4):
        ny = y + dy[k]
        nx = x + dx[k]
        if 0 <= ny < H and 0 <= nx < W:
            if graph[ny][nx] == '#':
                dfs(ny,nx)
                
for _ in range(T):
    H,W = map(int,input().split())
    graph = [list(input()) for _ in range(H)]
    count = 0
    for i in range(H):
        for j in range(W):
            if graph[i][j] == '#':
                dfs(i,j)
                count += 1
                
    print(count)