티스토리 뷰
문제
https://www.acmicpc.net/problem/10026
10026번: 적록색약
적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)
www.acmicpc.net
풀이
모든 좌표에 대해서 방문하지 않은 곳이면 깊이 우선 탐색을 진행한다.
다만, 적록색약과 아닌 시각을 구분해야 하기 때문에 visited 배열을 두 개 만들어 주었다.
또한 dfs함수에 color_type을 매개변수로 넘겨주어서 코드의 중복을 줄일 수 있었다.
코드
import sys
sys.setrecursionlimit(10**9)
N = int(input())
graph = [input() for _ in range(N)]
visited_color = [[False] * N for _ in range(N)]
visited_blind = [[False] * N for _ in range(N)]
color = {'R': 0, 'G': 1, 'B': 2}
blind = {'R': 1, 'G': 1, 'B': 2}
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
result = [0, 0]
def dfs(x: int, y: int, visited: list, color_type: dict):
visited[x][y] = True
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < N and 0 <= ny < N:
if not visited[nx][ny] and color_type[graph[x][y]] == color_type[graph[nx][ny]]:
dfs(nx, ny, visited, color_type)
def count(visited: list, color_type: dict):
for i in range(N):
for j in range(N):
if not visited[i][j]:
result[color_type['R']] += 1
dfs(i, j, visited, color_type)
count(visited_color, color)
count(visited_blind, blind)
print(*result)
'Problem Solving > 백준' 카테고리의 다른 글
백준 1005번 파이썬 (0) | 2022.09.12 |
---|---|
백준 2263번 파이썬 (0) | 2022.07.01 |
백준 4256번 파이썬 (0) | 2022.06.30 |
백준 3190번 파이썬 (0) | 2022.06.29 |