[Algorithm🧬] 정올 2765 : 미술관람 대회

또상·2022년 11월 2일
0

Algorithm

목록 보기
74/133
post-thumbnail

문제

import sys
from collections import deque
import copy

def BFS(picture, i, j, color):
    q = deque()
    q.append((i, j, color))
    picture[i][j] = 0

    while q:
        x, y, tcolor = q.popleft()

        for dx, dy in adj:
            nx = x + dx
            ny = y + dy

            if not 0 <= nx < N:
                continue
            if not 0 <= ny < N:
                continue

            if picture[nx][ny] != tcolor:
                continue

            q.append((nx, ny, picture[nx][ny]))
            picture[nx][ny] = 0




readl = sys.stdin.readline
N = int(readl())
picture = [[c for c in readl().strip()] for _ in range(N)]
copy = copy.deepcopy(picture)

adj = [(-1, 0), (1, 0), (0, -1), (0, 1)]

cnt = 0
rgcnt = 0

for i in range(N):
    for j in range(N):
        if picture[i][j] != 0:
            BFS(picture, i, j, picture[i][j])
            cnt += 1

print(cnt)


for i in range(N):
    for j in range(N):
        if copy[i][j] == 'G':
            copy[i][j] = 'R'

cnt = 0
for i in range(N):
    for j in range(N):
        if copy[i][j] != 0:
            BFS(copy, i, j, copy[i][j])
            cnt += 1

print(cnt)

BFS 함수를 일반이랑 돼지용으로 2개 만드는 방법도 있었을 것 같은데, 그냥 배열을 copy 해서 진행했다.

근데 복사를 하는데...? 왜 같이 바뀌지...?
copy on write 가 아닌가...? 하고 찾아봤더니 아니란다...

import copy
배열2 = copy.deepcopy(배열)

로 해야한단다...

profile
0년차 iOS 개발자입니다.

0개의 댓글