Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

짱이 될거야

백준 10026: 적록색약 Python 본문

알고리즘

백준 10026: 적록색약 Python

짱이 되었어 2022. 10. 24. 23:02

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

 

10026번: 적록색약

적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)

www.acmicpc.net

 

 

from collections import deque
import sys
input = sys.stdin.readline


def red_green_strong(si, sj):
    q = deque()
    q.append((si, sj))
    visited[si][sj] = 1

    while q:
        ci, cj = q.popleft()
        for di, dj in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            ni, nj = ci + di, cj + dj
            if 0 <= ni < N and 0 <= nj < N and not visited[ni][nj]:
                if grid[si][sj] == 'R':
                    if grid[ni][nj] == 'R':
                        q.append((ni, nj))
                        visited[ni][nj] = 1
                elif grid[si][sj] == 'G':
                    if grid[ni][nj] == 'G':
                        q.append((ni, nj))
                        visited[ni][nj] = 1
                elif grid[si][sj] == 'B':
                    if grid[ni][nj] == 'B':
                        q.append((ni, nj))
                        visited[ni][nj] = 1


def red_green_weak(si, sj):
    q = deque()
    q.append((si, sj))
    visited2[si][sj] = 1

    while q:
        ci, cj = q.popleft()
        for di, dj in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            ni, nj = ci + di, cj + dj
            if 0 <= ni < N and 0 <= nj < N and not visited2[ni][nj]:
                if grid[si][sj] == 'R' or grid[si][sj] == 'G':
                    if grid[ni][nj] == 'R' or grid[ni][nj] == 'G':
                        q.append((ni, nj))
                        visited2[ni][nj] = 1
                elif grid[si][sj] == 'B':
                    if grid[ni][nj] == 'B':
                        q.append((ni, nj))
                        visited2[ni][nj] = 1


N = int(input())    # N: 그리드 한 변의 크기
grid = [list(input().rstrip()) for _ in range(N)]
visited = [[0] * N for _ in range(N)]   # 적록색약 아닌 사람 방문체크
visited2 = [[0] * N for _ in range(N)]  # 적록색약인 사람 방문체크

cnt1, cnt2 = 0, 0
for i in range(N):
    for j in range(N):
        if not visited[i][j]:
            red_green_strong(i, j)
            cnt1 += 1

for i in range(N):
    for j in range(N):
        if not visited2[i][j]:
            red_green_weak(i, j)
            cnt2 += 1
print(cnt1, cnt2)
Comments