๋ฌธ์
๋ฐฑ์ค ์จ๋ผ์ธ ์ ์ง - 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()
๋ฐ์ํ
'๐ algorithm > boj' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ 6550 - ๋ถ๋ถ ๋ฌธ์์ด (0) | 2021.03.18 |
---|---|
BOJ 10819 - ์ฐจ์ด๋ฅผ ์ต๋๋ก (0) | 2021.03.18 |
BOJ 1449 - ์๋ฆฌ๊ณต ํญ์น (0) | 2021.03.18 |
BOJ 2805 - ๋๋ฌด ์๋ฅด๊ธฐ (0) | 2021.03.18 |
BOJ 1654 - ๋์ ์๋ฅด๊ธฐ (0) | 2021.03.18 |
๐ฌ ๋๊ธ