[Python] 백준10026 적록색약
문제
예제 입력
코드
import sys
from collections import deque
n = int(input())
# RGB
arr1 = [ [] * n for _ in range(n) ]
arr2 = [ [] * n for _ in range(n) ]
visit1 = [ [ False ] * n for _ in range(n) ]
visit2 = [ [ False ] * n for _ in range(n) ]
for i in range(n):
s = sys.stdin.readline().strip()
arr1[i] = list(s)
arr2[i] = list(s.replace('G', 'R'))
# 영역 체크
dy = [1, 0, -1, 0]
dx = [0, 1, 0, -1]
def BFS(y, x, arr, visit) :
dq = deque()
visit[y][x] = True
dq.append([y, x])
while dq :
oldy, oldx = dq.popleft()
for i in range(4):
newy = oldy + dy[i]
newx = oldx + dx[i]
if 0 <= newy < n and 0 <= newx < n:
if not visit[newy][newx] and arr[newy][newx] == arr[y][x]:
visit[newy][newx] = True
dq.append([newy, newx])
cnt1 = 0
for i in range(n):
for j in range(n):
if not visit1[i][j]:
BFS(i, j, arr1, visit1)
cnt1 += 1
cnt2 = 0
for i in range(n):
for j in range(n):
if not visit2[i][j]:
BFS(i, j, arr2, visit2)
cnt2 += 1
print(cnt1, cnt2)
설명
파이썬을 통해서 사용자로부터 입력받아 자료형 deque를 사용하여 적록색약를 구현했습니다.