문제 주소: https://www.acmicpc.net/problem/10026
난이도: gold 5
#필요한것들 import
from collections import deque
from copy import deepcopy
import sys
sys.setrecursionlimit(10000)
#입력받기
n = int(input())
graph = []
for i in range(n):
_temp = list(input())
graph.append(_temp)
#deepcopy로 값만 복사함, 색약 케이스를 계산할 자료
cw_graph = deepcopy(graph)
normal_cnt = 0
cw_cnt = 0
#dfs세팅
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
#dfs의 기본 개념 이용했으며 새로운 좌표가 그리드 내에 있는것 확인과 더불어 새로운 좌표의 색이 이전색과 같은지 확인
def dfs_normal(graph, x, y, prev):
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < n and graph[nx][ny] == prev:
if graph[nx][ny] == 'R':
graph[nx][ny] = '0'
dfs_normal(graph, nx, ny, 'R')
elif graph[nx][ny] == 'G':
graph[nx][ny] = '1'
dfs_normal(graph, nx, ny, 'G')
elif graph[nx][ny] == 'B':
graph[nx][ny] = '2'
dfs_normal(graph, nx, ny, 'B')
def dfs_cw(graph, x, y, prev):
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if prev == 'B': #이전색이 파란색인 경우
if 0 <= nx < n and 0 <= ny < n and graph[nx][ny] == prev:
if graph[nx][ny] == 'R':
graph[nx][ny] = '0'
dfs_cw(cw_graph, nx, ny, 'R')
elif graph[nx][ny] == 'G':
graph[nx][ny] = '1'
dfs_cw(cw_graph, nx, ny, 'G')
elif graph[nx][ny] == 'B':
graph[nx][ny] = '2'
dfs_cw(cw_graph, nx, ny, 'B')
else: # 이전색이 R또는 G인경우
if 0 <= nx < n and 0 <= ny < n and (graph[nx][ny] == 'R' or graph[nx][ny] == 'G'):
if graph[nx][ny] == 'R':
graph[nx][ny] = '0'
dfs_cw(cw_graph, nx, ny, 'R')
elif graph[nx][ny] == 'G':
graph[nx][ny] = '1'
dfs_cw(cw_graph, nx, ny, 'G')
elif graph[nx][ny] == 'B':
graph[nx][ny] = '2'
dfs_cw(cw_graph, nx, ny, 'B')
for x in range(n):
for y in range(n):
if graph[x][y].isalpha():
if graph[x][y] == 'R':
normal_cnt += 1
graph[x][y] = '0'
dfs_normal(graph, x, y, 'R')
elif graph[x][y] == 'G':
normal_cnt += 1
graph[x][y] = '1'
dfs_normal(graph, x, y, 'G')
elif graph[x][y] == 'B':
normal_cnt += 1
graph[x][y] = '2'
dfs_normal(graph, x, y, 'B')
for x in range(n):
for y in range(n):
if cw_graph[x][y].isalpha():
if cw_graph[x][y] == 'R':
cw_cnt += 1
cw_graph[x][y] = '0'
dfs_cw(cw_graph, x, y, 'R')
elif cw_graph[x][y] == 'G':
cw_cnt += 1
cw_graph[x][y] = '1'
dfs_cw(cw_graph, x, y, 'G')
elif cw_graph[x][y] == 'B':
cw_cnt += 1
cw_graph[x][y] = '2'
dfs_cw(cw_graph, x, y, 'B')
print(normal_cnt, cw_cnt)