Skip to main content

백준10026적록색약

·99 words·1 min· loading

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

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

dfs를 두번수행한다.

처음엔 색깔을 다 따로,

다음엔 초록,빨강을 묶어서

import sysfrom collections import dequeimport copyinput=sys.stdin.readline
move=((-1,0),(1,0),(0,-1),(0,1))def bfs1(i,j,color,arr):    que=deque()
    que.append((i,j))    arr[i][j]=-1    while que:        i,j=que.popleft()
        for a,b in move:            ti,tj=i+a,j+b
            if 0<=ti<n and 0<=tj<n and arr[ti][tj]==color:
                arr[ti][tj]=-1                que.append((ti,tj))n=int(input())
graph=[list(input().strip())for _ in range(n)]arr=copy.deepcopy(graph)
arr1=copy.deepcopy(graph)for i in range(n):    for j in range(n):
        if arr1[i][j] in ('R','G'):            arr1[i][j]='R'answer0=0answer1=0
for i in range(n):    for j in range(n):        if arr[i][j]!=-1:
            bfs1(i,j,arr[i][j],arr)            answer0+=1
        if arr1[i][j]!=-1:            bfs1(i,j,arr1[i][j],arr1)
            answer1+=1print(answer0,answer1)