본문 바로가기

성우리뷰

현대 이미지 프로세싱

import sys
#sys.setrecursionlimit(10**5)

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

board = []
maxy, maxx = map(int, input().split())
visit = [[False]*maxx for _ in range(maxy)]

for i in range(maxy):
    content = list(map(int,sys.stdin.readline().split()))
    board.append(content)

loop = int(input())
for i in range(loop):
    y, x, sowhat = map(int, input().split())
    past = board[y-1][x-1]
    vec = [(y-1,x-1)]
    board[y-1][x-1] = sowhat

    while len(vec) > 0 :
        y, x = vec[0]
        vec.pop(0)
        visit[y][x] = True

        for j in range(4):
            ny, nx = y + dy[j], x + dx[j]
            if ny >= maxy or nx >= maxx or ny < 0 or nx < 0:
                continue
            if board[ny][nx] != past:
                continue
            if visit[ny][nx] == True:
                continue
            board[ny][nx] = sowhat
            vec.append((ny, nx))
            
    for i in range(len(board)):
        for j in range(len(board[i])):
            visit[i][j] = False

for i in range(len(board)):
    for j in range(len(board[i])):
        print(board[i][j], end = ' ')
    print()

같은 숫자들이 들어오면 무한루프 주의. 따라서 visit 통해 점검하는 게 중요.

또는

import sys
sys.setrecursionlimit(10**5)

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

board = []
maxy, maxx = map(int, input().split())
visit = [[False]*maxx for _ in range(maxy)]

def color(y, x, c):
    oldc = board[y][x]
    board[y][x] = c
    for j in range(4):
        ny, nx = y + dy[j], x + dx[j]
        if ny >= maxy or nx >= maxx or ny < 0 or nx < 0:
                continue
        if board[ny][nx] == oldc:
            color(ny, nx, c)
    return

for i in range(maxy):
    content = list(map(int,sys.stdin.readline().split()))
    board.append(content)

loop = int(input())
for i in range(loop):
    y, x, sowhat = map(int, input().split())
    y, x = y-1, x-1
    if board[y][x] != sowhat:
        color(y,x,sowhat)

for i in range(len(board)):
    for j in range(len(board[i])):
        print(board[i][j], end = ' ')
    print()

근데 틀렸다. 어디가 틀린진 모르겟음. 테스트케이스 중 몇 개가 시간초과남. 일단 다 정답이긴 함.

밑에 건 공식정답.

sys,setrecursionlimit(10**5)를 통해 최대 재귀횟수를 늘린 다음,

nx, ny를 거치지 않고, 처음부터 겉면에 0의 padding을 넣은 배열로 시작한다.

import sys
sys.setrecursionlimit(10**5)

def color(y, x, c):
    oldc = board[y][x]
    board[y][x] = c
    if board[y-1][x] == oldc:
        color(y-1, x, c)
    if board[y+1][x] == oldc:
        color(y+1, x, c)
    if board[y][x-1] == oldc:
        color(y, x-1, c)
    if board[y][x+1] == oldc:
        color(y, x+1, c)
    return

maxy, maxx = map(int, input().split())
board = [[0] * (maxx+2)]
for i in range(maxy):
    content = list(map(int,sys.stdin.readline().split()))
    board.append([0] + content + [0])
board.append([0] * (maxx+2))

loop = int(input())
for i in range(loop):
    y, x, sowhat = map(int, input().split())
    if board[y][x] != sowhat:
        color(y,x,sowhat)

for i in range(1, maxy+1):
    for j in range(1, maxx+1):
        print(board[i][j], end = ' ')
    print()

'성우리뷰' 카테고리의 다른 글

[softeer] 성적평가  (0) 2023.04.03
[Softeer] 금고 관리  (0) 2023.03.27
현대 좌석관리  (0) 2022.03.10
start, end 이중점 이용 마이크로서비스  (0) 2022.03.05
현대 교차로  (0) 2022.02.22