import sys
from collections import deque
#좌우상하
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
def bfs(i, j, c):
oc = maps[i-1][j-1]
if oc == c: # 색깔이 같으면 진행 안해도 된다.
return
queue = deque()
queue.append([i-1, j-1])
while queue:
x, y = queue.popleft()
maps[x][y] = c
for k in range(4):
nx, ny = x + dx[k], y + dy[k]
if 0<=nx<h and 0<=ny<w:
if maps[nx][ny] == oc:
maps[nx][ny] = c
queue.append([nx, ny])
h, w = map(int, sys.stdin.readline().split())
maps = [list(map(int, sys.stdin.readline().split())) for _ in range(h)]
q = int(sys.stdin.readline().rstrip())
for _ in range(q):
i, j, c = map(int, sys.stdin.readline().split())
bfs(i, j, c)
for line in maps:
print(' '.join([str(val) for val in line]))
출력할 때 애먹었던 문제.
for i in range(h):
for j in range(w):
print(maps[i][j], end=" ")
print()
이렇게 하면 결과는 똑같은 것 같은데 테스트 값이 다르다고 나온다.
값을 string형식으로 바꿔서 ' '.join()
을 이용해야 맞다고 나온다.
Python
기본적으로 파이썬은 개행이 자동으로 이루어진다.
end="\n"이 포함되어 있기 때문인데 end=" " 하면 공백을 넣어서 한 줄에 출력해준다.
'구분자'.join(리스트)
: 문자열 합치기
''.join(['a', 'b', 'c']) => abc