From da6c3f24b937d351da8275b40b68ae4746dd7900 Mon Sep 17 00:00:00 2001 From: kyu-hyun <102718303+lalabulla@users.noreply.github.com> Date: Sun, 8 Oct 2023 10:11:03 +0900 Subject: [PATCH] =?UTF-8?q?Create=20=ED=86=A0=EB=A7=88=ED=86=A0=20kyuhyun.?= =?UTF-8?q?py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kyuhyun.py" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 "Baekjoon - \353\254\270\354\240\234\355\222\200\354\235\264/\355\206\240\353\247\210\355\206\240/kyuhyun.py" diff --git "a/Baekjoon - \353\254\270\354\240\234\355\222\200\354\235\264/\355\206\240\353\247\210\355\206\240/kyuhyun.py" "b/Baekjoon - \353\254\270\354\240\234\355\222\200\354\235\264/\355\206\240\353\247\210\355\206\240/kyuhyun.py" new file mode 100644 index 00000000..1f4529ca --- /dev/null +++ "b/Baekjoon - \353\254\270\354\240\234\355\222\200\354\235\264/\355\206\240\353\247\210\355\206\240/kyuhyun.py" @@ -0,0 +1,46 @@ +from collections import deque +m, n = map(int, input().split(" ")) + +tomato = [] +q = deque() +flag = 0 + +for _ in range(n) : + tmp = list(map(int, input().split(" "))) + tomato.append(tmp) + +dx = [1, -1, 0, 0] +dy = [0, 0, 1, -1] + +def bfs(): + global res + while q: + time, y, x = q.popleft() + res = time + + 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 tomato[ny][nx] == 1 or tomato[ny][nx] == -1: + continue + q.append([time + 1, ny, nx]) + tomato[ny][nx] = 1 + +for i in range(n): + for j in range(m): + if tomato[i][j] == 1: + q.append([0, i, j]) +bfs() +for i in range(n): + if 0 in tomato[i]: + flag = 1 + break + +if flag == 1: + print(-1) +else: + print(res) +