boj 10026 골드5

김준오·2022년 4월 22일
0

알고리즘

목록 보기
82/91
post-thumbnail

문제

https://www.acmicpc.net/problem/10026

풀이

n = int(input())

arr = [list(input()) for _ in range(n)]
arr2 = [arr[i][:] for i in range(n)]

color = ['R','G','B']
dy = [0,1,0,-1]
dx = [1,0,-1,0]

# 적록색약 ver arr2
for i in range(n):
    for j in range(n):
        if arr[i][j] == 'R':
            arr2[i][j] = 'G'

def dfs(arr,y,x,c):
    q = []
    arr[y][x] = '0'
    q.append((y,x))

    while(q):
        a,b = q.pop()
        for i in range(4):
            ny, nx = a + dy[i], b + dx[i]
            if 0 <= ny < n and 0 <= nx < n and arr[ny][nx] == c:
                arr[ny][nx] = '0'
                q.append((ny,nx))

cnt = 0
for i in range(n):
    for j in range(n):
        if arr[i][j] != '0':
            dfs(arr,i,j,arr[i][j])
            cnt += 1

cnt2 = 0
for i in range(n):
    for j in range(n):
        if arr2[i][j] != '0':
            dfs(arr2,i,j,arr2[i][j])
            cnt2 += 1

print(cnt,end=' ')
print(cnt2)

그냥 평범한 dfs

profile
jooooon

0개의 댓글

관련 채용 정보