๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 15240 - Paint bucket

by HandHand 2021. 3. 18.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 15240๋ฒˆ

ํ’€์ด ๊ณผ์ •

pixels ๋ผ๋Š” ์ด์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ ๋‹ค์Œ, ์‹œ์ž‘ ์ง€์ ์—์„œ BFS ๋ฅผ ํ†ตํ•ด ๋ฐฉ๋ฌธ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
์ด๋•Œ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ํ•˜๊ธฐ ์ „์— ์ด์ „ ์œ„์น˜์˜ ํ”ฝ์…€ ๊ฐ’์„ ์ €์žฅํ•ด์„œ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์— ์œ ์˜ํ•ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys
from collections import deque


dr = [0, 0, 1, -1]
dc = [1, -1, 0, 0]


def bfs(r, c, color, pixels):
    q = deque()
    visit = [[0] * C for _ in range(R)]

    q.append([r, c])
    visit[r][c] = color

    while q:
        r, c = q.popleft()
        before = pixels[r][c]
        pixels[r][c] = color

        for i in range(4):
            nr, nc = r + dr[i], c + dc[i]
            if 0 <= nr < R and 0 <= nc < C:
                if pixels[nr][nc] == before and not visit[nr][nc]:
                    visit[nr][nc] = 1
                    q.append([nr, nc])

    return pixels


def solution():
    answer = bfs(Y, X, K, pixels)

    for r in range(R):
        print(''.join(map(str, answer[r])))


if __name__ == '__main__':
    R, C = list(map(int, sys.stdin.readline().split()))
    pixels = [list(sys.stdin.readline().strip()) for _ in range(R)]
    Y, X, K = list(map(int, sys.stdin.readline().split()))

    solution()
๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€