cmod.ify

[10026] 적록색약 본문

BASIC/코딩테스트

[10026] 적록색약

modifyC 2026. 1. 8. 17:27
728x90
반응형

초안

bfs를 두개 만들기 귀찮아서 한 색을 다 r로 바꿔버림

근데 다른 문법적 실수 때문인 가 싶어서 결국 bfs 다시 만듦

근데 그냥 방문처리 잘 못했던 실수였다.

그래서 두 번 푼 사람이 되어버렸음 ㅠㅠ

import sys
from collections import deque

input = sys.stdin.readline

n = int(input())

color = []

for i in range(n):
    l = input().strip()
    color.append(l)


visited = [[False] * n for _ in range(n)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

flag = ""


def bfs(x, y, col):
    cnt = 0
    q = deque([(x, y)])
    visited[x][y] = True
    while q:
        cx, cy = q.popleft()
        for i in range(4):
            nx = cx + dx[i]
            ny = cy + dy[i]
            if 0 <= nx < n and 0 <= ny < n and visited[nx][ny] == False:
                if color[nx][ny] == col:
                    visited[nx][ny] = True
                    q.append((nx, ny))
                    cnt += 1
    return cnt


def rgbfs(x, y, col):
    q = deque([(x, y, col)])
    visited[x][y] = True
    while q:
        cx, cy, cc = q.popleft()
        for i in range(4):
            nx = cx + dx[i]
            ny = cy + dy[i]
            if 0 <= nx < n and 0 <= ny < n and visited[nx][ny] == False:
                if (
                    color[nx][ny] == cc
                    or (color[nx][ny] == "G" and cc == "R")
                    or (color[nx][ny] == "R" and cc == "G")
                ):
                    visited[nx][ny] = True
                    q.append((nx, ny, color[nx][ny]))


rgb = 0
for i in range(n):
    for j in range(n):
        if visited[i][j] == False:
            col = color[i][j]
            bfs(i, j, col)
            rgb += 1


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

rrb = 0

for i in range(n):
    for j in range(n):
        if visited[i][j] == False:
            col = color[i][j]
            rgbfs(i, j, col)
            rrb += 1
print(f"{rgb} {rrb}")
import sys
from collections import deque

input = sys.stdin.readline

n = int(input())

color = []

for i in range(n):
    l = input().strip()
    color.append(l)


visited = [[False] * n for _ in range(n)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

flag = ""


def bfs(x, y, col):
    cnt = 0
    q = deque([(x, y)])
    visited[x][y] = True
    while q:
        cx, cy = q.popleft()
        for i in range(4):
            nx = cx + dx[i]
            ny = cy + dy[i]
            if 0 <= nx < n and 0 <= ny < n and visited[nx][ny] == False:
                if color[nx][ny] == col:
                    visited[nx][ny] = True
                    q.append((nx, ny))
                    cnt += 1
    return cnt


rgb = 0
for i in range(n):
    for j in range(n):
        if visited[i][j] == False:
            col = color[i][j]
            cnt = bfs(i, j, col)
            rgb += 1

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

rrb = 0
for i in range(n):
    for j in range(n):
        if color[i][j] == "G":
            color[i] = color[i].replace("G", "R")

for i in range(n):
    for j in range(n):
        if visited[i][j] == False:
            col = color[i][j]
            cnt = bfs(i, j, col)
            rrb += 1
print(f"{rgb} {rrb}")
728x90
반응형

'BASIC > 코딩테스트' 카테고리의 다른 글

[7662] 이중 우선순위 큐  (0) 2026.01.13
[16928] 뱀과 사다리 게임  (2) 2026.01.09
[7569] 토마토 - 6방향 탐색  (0) 2026.01.08
[5430] AC  (0) 2026.01.07
[11403] 경로 찾기 - 플로이드-워셜  (0) 2026.01.07