내용으로 건너뛰기
테페리넷
사용자 도구
등록
로그인
사이트 도구
검색
도구
문서 보기
Fold/unfold all
역링크
미디어 관리자
사이트맵
등록
로그인
>
미디어 관리자
사이트맵
현재 위치:
테페리넷
»
Problem Solving
»
문제
»
백준 온라인 저지 (BOJ)
»
토마토
ps:problems:boj:7569
이 문서는 읽기 전용입니다. 원본을 볼 수는 있지만 바꿀 수는 없습니다. 문제가 있다고 생각하면 관리자에게 문의하세요.
====== 토마토 ====== ===== 풀이 ===== * [[ps:problems:boj:7576]] 문제가 3차원으로 확장된 문제. 동일한 방법으로 BFS를 돌려서 풀면 된다. 해설은 그쪽을 참조. * 시간 복잡도는 똑같이 O(셀의 갯수) = O(MNH) ===== 코드 ===== <dkpr py> """Solution code for "BOJ 7576. 토마토". - Problem link: https://www.acmicpc.net/problem/7576 - Solution link: http://www.teferi.net/ps/problems/boj/7576 Tags: [BFS] """ from teflib import search RIPE = '1' UNRIPE = '0' EMPTY = '-1' def main(): def adj_tomatoes(cur_pos): p, x = divmod(cur_pos, M) z, y = divmod(p, N) delta = 1 for coord_i, size_i in ((x, M), (y, N), (z, H)): if coord_i > 0: next_pos = cur_pos - delta if tomatoes[next_pos] == UNRIPE: yield next_pos if coord_i < size_i - 1: next_pos = cur_pos + delta if tomatoes[next_pos] == UNRIPE: yield next_pos delta *= size_i M, N, H = [int(x) for x in input().split()] tomatoes = [] for _ in range(N * H): tomatoes.extend(input().split()) ripe_tomatoes = [i for i, tom in enumerate(tomatoes) if tom == RIPE] tomato_count = sum(1 for tom in tomatoes if tom != EMPTY) dists = search.min_distances(adj_tomatoes, ripe_tomatoes) print(-1 if len(dists) < tomato_count else max(dists.values())) if __name__ == '__main__': main() </dkpr> * Dependency: [[:ps:teflib:search#min_distances|teflib.search.min_distances]] {{tag>BOJ ps:problems:boj:실버_1}}
ps/problems/boj/7569.txt
· 마지막으로 수정됨: 2021/07/22 08:32 저자
teferi
문서 도구
문서 보기
역링크
Fold/unfold all
맨 위로