https://www.acmicpc.net/problem/10026
import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
n = int(input())
arr = [list(input().rstrip()) for _ in range(n)]
# 적록색약: R = G
# 정상: R, G, B
not_blind, blind = 0,0
visited = [[0]*n for _ in range(n)]
b_visited = [[0]*n for _ in range(n)]
# 정상
def dfs(i,j, color):
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
nx, ny = i + dx, j + dy
if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny] and arr[nx][ny] == color:
visited[nx][ny] = 1
dfs(nx,ny, arr[nx][ny])
# 정상
for i in range(n):
for j in range(n):
if not visited[i][j]:
dfs(i,j, arr[i][j])
not_blind += 1
# 색약
def b_dfs(i,j, color):
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
nx, ny = i + dx, j + dy
if 0 <= nx < n and 0 <= ny < n and not b_visited[nx][ny]:
if color == arr[nx][ny] or (color != 'B' and arr[nx][ny] != 'B'):
b_visited[nx][ny] = 1
b_dfs(nx,ny, arr[nx][ny])
# 색약
for i in range(n):
for j in range(n):
if not b_visited[i][j]:
b_dfs(i,j, arr[i][j])
blind += 1
print(not_blind, blind)
'백준' 카테고리의 다른 글
백준 9466. 텀 프로젝트 (0) | 2025.01.30 |
---|---|
백준 5427. 불 (0) | 2025.01.29 |
10971. 외판원 순회 2 (0) | 2025.01.28 |
백준 11724. 연결 요소의 개수 (0) | 2025.01.28 |
백준 4179. 불! (0) | 2025.01.26 |