백준 10026 python [적록색약]

인지용·2025년 1월 5일
0

알고리즘

목록 보기
17/46
post-thumbnail

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

from collections import deque
from copy import deepcopy

# with open("./data.txt") as file:
#     lines = file.read().strip().split("\n")
    
    
# N = int(lines[0])
N = int(input())
normalCnt = 0
cnt = 0
moveX = [1, 0, -1, 0]
moveY = [0, -1, 0, 1]
originPicture = []
result = ""

for i in range(N):
    # originPicture.append(list(lines[i+1].strip()))
    originPicture.append(list(input().strip()))

def bfs(x, y):
    Q = deque()
    Q.append((x, y))
    visited[x][y] = True
    
    while Q:
        nowX, nowY = Q.popleft()
        
        for i in range(4):
            nextX = nowX + moveX[i]
            nextY = nowY + moveY[i]
            
            if(nextX >= N or nextY >= N or nextX < 0 or nextY < 0):
                continue
            
            if(visited[nextX][nextY]):
                continue

            if(picture[nowX][nowY] == picture[nextX][nextY]):
                Q.append((nextX, nextY))
                visited[nextX][nextY] = True            


for i in range(2):
    picture = deepcopy(originPicture)
    visited = [[False for _ in range(N)] for _ in range(N)]

    if(i == 1):
        for a in range(N):
            for b in range(N):
                if(picture[a][b] == 'G'):
                    picture[a][b] = 'R'
    
    for j in range(N):
        for k in range(N):
            if(visited[j][k] == False):
                bfs(j, k)
                cnt += 1
                
    if(i == 0):
        result += str(cnt)
    else:
        result += " " + str(cnt)
    cnt = 0
    
print(result)

접근 방법

이번 문제는 어떤 방식으로 R과 G를 동일하게 보게 할까였던 것 같다.

나는 문자열을 치환해버렸다.

그러면 나머지 로직은 다른 문제들과 거의 비슷하게 흘러간다.

그리고 원본 배열을 만들어놓고 깊은 복사를 사용하여 새로운 배열을 만들고 치환했다.

visited = [[False] * N] * N 

아무튼 엄청 어렵진 않은 문제였던 것 같다.

profile
한-줄

0개의 댓글