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

 

10026번: 적록색약

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

www.acmicpc.net

 

솔루션 :

1. 배열을 각각 2개를 만든다(R-G-B배열, RG-B배열)

2. 모든 위치에 대해서 BFS를 진행

 

소스코드 : 

N = int(input())

# Red, Green, Blue
normal_maps = []
normal_TF = [[False for _ in range(N)] for _ in range(N)] 

# Mixed, Blue
redgreen_maps = []
redgreen_TF = [[False for _ in range(N)] for _ in range(N)] 

#입력받는부분
for i in range(N) :
    temp = list(map(str,input()))
    normal_maps.append(temp)
import copy
redgreen_maps = copy.deepcopy(normal_maps)

# Mixed수정해주는 부분
for i in range(0,N) :
    for j in range(0, N) :
        if (normal_maps[i][j] == 'R') or (normal_maps[i][j] == 'G') :
            redgreen_maps[i][j] = 'W'


#BFS구성
from collections import deque
def bfs(y,x,what_map,what_TF) :
    
    Q = deque()
    Q.append((y,x))

    dy = [-1,0,1,0]
    dx = [0,1,0,-1]

    while Q :
        
        y,x = Q.popleft()
        what_TF[y][x] = True

        for i in range(4) :
            ny = y + dy[i]
            nx = x + dx[i]

            if nx<0 or nx>=N or ny<0 or ny>=N :
                continue 

            if what_map[ny][nx] != what_map[y][x] : #색깔이 다른 경우
                continue

            if what_TF[ny][nx] == True : #이미가봐서 True인경우
                continue 


            what_TF[ny][nx] = True #아직안가본곳을 True로설정,
            Q.append((ny,nx)) #다음 큐에 넣음

#Bfs를 얼마나 진입했는지 카운팅
normal_cnt = 0
redgreen_cnt = 0

#전체를 돌면서 (좌표,좌표,맵, TF) 를 넣어줌
#한번진행할때, 동일한 색상에 대해서 BFS를 진행, cnt+=1 진행
for i in range(N) :
    for j in range(N) :
        if (normal_TF[i][j]==False) : 
            bfs(i,j,normal_maps,normal_TF)
            normal_cnt +=1

        if (redgreen_TF[i][j]==False) : 
            bfs(i,j,redgreen_maps,redgreen_TF)
            redgreen_cnt +=1


print(normal_cnt)
print(redgreen_cnt)

'CodingTest' 카테고리의 다른 글

SWEA 1234 [S/W 문제해결 기본] 10일차 - 비밀번호  (0) 2022.06.05
SWEA 14178. 1차원 정원 파이썬  (0) 2022.06.05
[BOJ] 20922 겹치는건 싫어  (0) 2022.02.19
[BOJ] 11403 경로 찾기  (0) 2022.02.19
[BOJ] 14502 연구소  (0) 2022.02.19
jjongguet