인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

pionyline1344's profile image
pionyline1344

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

BFS 토마토

Written on

·

161

0

혹시 토마토 상자가 3차원 배열인 경우에는 어떤식으로 이동할 지 알 수 있을까요? 

이동 좌표는 아래와 같은데 층 이동을 어떻게 하는지 모르겠습니다.

총 2층인 3차원 배열 중 만약 익은 토마토가 있는 곳에서 시작되는 층이 0층인 경우 -1층을 할수가 있는건가요?ㅠ 

그리고 만약 0층에서 1층으로 올라갈 경우 좌표가 어디서 시작하는지도 궁금합니다. 

dx=[-1,0,1,0,0,0] :행

dy=[0,1,0,-1,0,0] :열

hh=[0,0,0,0,-1,1] : 높이

이것저것 자료를 찾아봐도 명확하게 알기가 어려워 그림으로 간단하게 알려주실수 있으신지 문의드립니다..

입력값은 아래와 같습니다. 

5=가로 3=세로 2=높이
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
python코테 준비 같이 해요!

Answer 1

1

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

백준 7569번 소스입니다.

import sys
from collections import deque
m,n,h = map(int,input().split()) # mn크기, h상자수
graph = []
queue = deque([])
 
for i in range(h):
    tmp = []
    for j in range(n):
        tmp.append(list(map(int,sys.stdin.readline().split())))
        for k in range(m):
            if tmp[j][k]==1:
                queue.append([i,j,k])
    graph.append(tmp)
    
dx = [-1,1,0,0,0,0]
dy = [0,0,1,-1,0,0]
dz = [0,0,0,0,1,-1]
while(queue):
    x,y,z = queue.popleft()
    
    for i in range(6):
        a = x+dx[i]
        b = y+dy[i]
        c = z+dz[i]
        if 0<=a<and 0<=b<and 0<=c<and graph[a][b][c]==0:
            queue.append([a,b,c])
            graph[a][b][c] = graph[x][y][z]+1
            
day = 0
for i in graph:
    for j in i:
        for k in j:
            if k==0:
                print(-1)
                exit(0)
        day = max(day,max(j))
print(day-1)
pionyline1344's profile image
pionyline1344

asked

Ask a question