티스토리 뷰
from collections import deque
n, m = map(int, input().split())
graph = []
for i in range(m):
graph.append(list(map(int, input().split())))
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
queue = deque()
def bfs():
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or nx >= m or ny < 0 or ny >= n:
continue
if graph[nx][ny] == 0:
graph[nx][ny] = graph[x][y] + 1
queue.append([nx, ny])
count = 0
before = 0
for i in range(m):
for j in range(n):
if graph[i][j] == 1:
queue.append([i, j])
bfs()
max_ = 0
for i in range(m):
maxtmp = max(graph[i])
if 0 in graph[i]:
max_ = 0
break
if maxtmp > max_:
max_ = maxtmp
print(max_-1)
DFS로는 풀 수 없고 BFS로만 풀 수 있는 문제.
DFS로 풀 수 없는 이유는, 하나의 1에서 시작하면 한없이 깊이 파고 들어서 여러개의 1(토마토)가 있을 때에도 하나의 1에 대해서 깊이 하고 들기 때문이다.
'코딩테스트 대비' 카테고리의 다른 글
[백준] 1697번 숨바꼭질 (0) | 2022.02.22 |
---|---|
[백준] 7569번 토마토 3차원 (0) | 2022.02.21 |
[백준] 2178번 미로 탐색 (0) | 2022.02.21 |
[백준] 1012번 유기농 배추 (0) | 2022.02.21 |
[백준] 2667번 단지번호 붙이기 (0) | 2022.02.21 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 다익스트라
- 설치하기
- 카카오
- torchscript
- numpy
- LGSVL
- BFS
- 프로그래머스
- 최소신장트리
- version
- 파이썬
- error
- n과m
- pytorch
- CUDA
- 백트래킹
- matplotlib
- 동적프로그래밍
- 코딩테스트
- tensorflow
- PIP
- 백준
- torch
- shellscript
- 이것이코딩테스트다
- dfs
- docker
- Python
- 설치
- notfound
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함