BOJ - 15240 - Paint bucket

Updated:

import sys
from collections import deque

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

def BFS(matrix, X, Y, K, R, C):
    visit = [[False] * C for _ in range(R)]
    visit[X][Y] = True

    dq = deque()
    dq.append((X, Y))

    while dq:
        x, y = dq.popleft()
        color = matrix[x][y]
        matrix[x][y] = K

        for k in range(4):
            nx, ny = x + dx[k], y + dy[k]
            if 0 <= nx < R and 0 <= ny < C:
                if matrix[nx][ny] == color and not visit[nx][ny]:
                    dq.append((nx, ny))
                    visit[nx][ny] = True

def solution():
    R, C = map(int, sys.stdin.readline().split())

    matrix = []
    for _ in range(R):
        matrix.append(list(sys.stdin.readline().strip()))

    X, Y, K = map(int, sys.stdin.readline().split())

    BFS(matrix, X, Y, str(K), R, C)

    for i in range(R):
        for j in range(C):
            print(matrix[i][j], end='')
        print()

solution()

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

Categories:

Updated:

Leave a comment