[Python] 소프티어 LV.3_이미지프로세싱

szlee·2023년 11월 5일
0

알고리즘 PS

목록 보기
5/12

소프티어 LV.3_이미지프로세싱


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

profile
🌱

0개의 댓글