토마토
틀렸다고 뜨지만 뭐가틀렸는지 모르겠고 안알려주니까 좆같으니까 패쓰. 솔직히 이렇게 해낸게 기특함.
# 6 4
# 0 0 2 0 0 0
# 0 2 1 2 0 0
# 0 0 2 0 0 0
# 0 0 0 0 0 1
# visited 몇번째 만에 방문한것인지 확인.
# 여러개 일수 있기때문이 몇번째 레벨에서 방문했는지를 확인할 수있다.
col, row = map(int, input().split()) # col = 6, row = 4
tomato = [] # 토마토가 있는곳은 '0'이나'1' 없는곳은 -1
visited = [[0]*col for _ in range(row)] # 방문한곳에 숫자
for i in range(row):
tomato.append(input().split())
dir = [[-1,0],[1,0],[0,-1],[0,1]]
def bfs():
now = -1
todo = []
for i in range(row): # 0 ~ 3
for j in range(col): # 0 ~ 5
if tomato[i][j] == '1':
todo.append([i,j])
if len(todo) == 0:
return -1
while todo:
curr = todo.pop(0) # [3,5]
x = curr[0] # 3
y = curr[1] # 5
if now < visited[x][y]: # -1 < 1
now = visited[x][y] # now = 1
for d in dir: # [[-1,0],[1,0],[0,-1],[0,1]]
wx = x + d[0] # 3-1 row
wy = y + d[1] # 5+0 col
if wx < 0 or wy < 0 or wx >= row or wy >= col:
# print(str([wx,wy]) + "not allowed")
continue
# 토마토가 아직 안익었고, 방문한적이 없어야함.
if tomato[wx][wy] == '0' and visited[wx][wy] == 0:
tomato[wx][wy] = '1'
visited[wx][wy] = visited[x][y] + 1
todo.append([wx,wy])
return now
print(bfs())
# [
# [9, 8, 7, 6, 5, 4],
# [8, 7, 6, 5, 4, 3],
# [7, 6, 5, 4, 3, 2],
# [6, 5, 4, 3, 2, 1]
# ]
# [
# [8, 7, 6, 5, 4, 3],
# [7, 6, 5, 4, 3, 2],
# [6, 5, 4, 3, 2, 1],
# [5, 4, 3, 2, 1, 0]
# ]
# [
# [0, 1, 2, 3, 4, 3],
# [1, 2, 3, 4, 3, 2],
# [2, 3, 4, 3, 2, 1],
# [3, 4, 3, 2, 1, 0]
# ]
# [
# [0, 0, 0, 0, 0, 0],
# [0, 4, 3, 2, 3, 0],
# [0, 3, 2, 1, 2, 0],
# [0, 0, 0, 0, 1, 0]
# ]