BOJ - 10026 - 적록색약

Updated:

from collections import deque

def BFS(i, j, RGB, Check, N):
    dx = [1, -1, 0, 0]
    dy = [0, 0, 1, -1]

    dq = deque()
    dq.append((i, j))
    Check[i][j] = True

    while dq:
        x, y = dq.popleft()
        for step in range(4):
            nx, ny = x + dx[step], y + dy[step]
            if nx < 0 or nx >= N or ny < 0 or ny >= N:
                continue

            if not Check[nx][ny] and RGB[nx][ny] == RGB[i][j]:
                Check[nx][ny] = True
                dq.append((nx, ny))

    return 1

def RGBBFS(i, j, RGB, Check, N):
    dx = [1, -1, 0, 0]
    dy = [0, 0, 1, -1]

    dq = deque()
    dq.append((i, j))
    Check[i][j] = True

    while dq:
        x, y = dq.popleft()
        for step in range(4):
            nx, ny = x + dx[step], y + dy[step]
            if nx < 0 or nx >= N or ny < 0 or ny >= N:
                continue

            if not Check[nx][ny]:
                if RGB[i][j] == "R" or RGB[i][j] == "G":
                    if RGB[nx][ny] == "R" or RGB[nx][ny] == "G":
                        Check[nx][ny] = True
                        dq.append((nx, ny))
                else:
                    if RGB[nx][ny] == RGB[i][j]:
                        Check[nx][ny] = True
                        dq.append((nx, ny))

    return 1

def solution():
    N = int(input())
    RGB = []
    Check = [[False for _ in range(N)] for _ in range(N)]
    RGBCheck = [[False for _ in range(N)] for _ in range(N)]
    for _ in range(N):
        RGB.append(list(str(input())))

    ret = 0
    retRGB = 0
    for i in range(N):
        for j in range(N):
            if not Check[i][j]:
                ret += BFS(i, j, RGB, Check, N)
            if not RGBCheck[i][j]:
                retRGB += RGBBFS(i, j, RGB, RGBCheck, N)
    print(ret, retRGB)
solution()

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

Categories:

Updated:

Leave a comment