https://www.acmicpc.net/problem/10026
10026번: 적록색약
적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)
www.acmicpc.net
- 일반적인 BFS를 구해주고
- 기존 graph에서 모든 R을 G로 바꾼 뒤 다시 BFS 돌림
from collections import deque
N = int(input())
graph = []
for _ in range(N):
graph.append(input())
visited = [[False] * N for _ in range(N)]
answer = [0] * 2
dx = [1,-1,0,0]
dy = [0,0,-1,1]
queue = deque()
def bfs(i,j,color):
if visited[i][j]:
return
queue.append((i,j))
visited[i][j] = True
while(queue):
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or nx >= N or ny < 0 or ny >= N:
continue
if not visited[nx][ny] and graph[nx][ny] == color:
visited[nx][ny] = True
queue.append((nx, ny))
for i in range(N):
for j in range(N):
if not visited[i][j]:
answer[0] += 1
bfs(i,j,graph[i][j])
for i in range(N):
graph[i] = graph[i].replace('R', 'G')
visited = [[False] * N for _ in range(N)]
for i in range(N):
for j in range(N):
if not visited[i][j]:
answer[1] += 1
bfs(i,j,graph[i][j])
for i in answer:
print(i, end=' ')
'◦ Algorithm > Python' 카테고리의 다른 글
프로그래머스 가장 긴 팰린드롬 문자열 (1) | 2023.04.15 |
---|---|
프로그래머스 디스크 컨트롤러 최소힙 (0) | 2023.04.12 |
프로그래머스 신고 결과 받기 구현..? (0) | 2023.04.11 |
프로그래머스 모음사전 완전탐색/DFS (0) | 2023.04.06 |
백준 안전영역 2468 BFS (0) | 2023.04.04 |