diff --git a/Seol-Munhyeok/.cph/.ATM.py_0961ba91b3ef35ddeba6bb2caf378d6c.prob b/Seol-Munhyeok/.cph/.ATM.py_0961ba91b3ef35ddeba6bb2caf378d6c.prob new file mode 100644 index 0000000..a55e59e --- /dev/null +++ b/Seol-Munhyeok/.cph/.ATM.py_0961ba91b3ef35ddeba6bb2caf378d6c.prob @@ -0,0 +1 @@ +{"name":"Local: ATM","url":"d:\\AlgoLeadME-15\\AlgoLeadMe-15\\Seol-Munhyeok\\ATM.py","tests":[{"id":1756627725177,"input":"5\n3 1 4 3 2","output":"32"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\AlgoLeadME-15\\AlgoLeadMe-15\\Seol-Munhyeok\\ATM.py","group":"local","local":true} \ No newline at end of file diff --git "a/Seol-Munhyeok/.cph/.\352\260\234\353\230\245\353\262\214\353\240\210.py_478b34ffa2be96fa5b5b2fbb1687d64c.prob" "b/Seol-Munhyeok/.cph/.\352\260\234\353\230\245\353\262\214\353\240\210.py_478b34ffa2be96fa5b5b2fbb1687d64c.prob" new file mode 100644 index 0000000..bdeb570 --- /dev/null +++ "b/Seol-Munhyeok/.cph/.\352\260\234\353\230\245\353\262\214\353\240\210.py_478b34ffa2be96fa5b5b2fbb1687d64c.prob" @@ -0,0 +1 @@ +{"name":"Local: 개똥벌레","url":"d:\\AlgoLeadME-15\\AlgoLeadMe-15\\Seol-Munhyeok\\개똥벌레.py","tests":[{"id":1756972182171,"input":"14 5\n1\n3\n4\n2\n2\n4\n3\n4\n3\n3\n3\n2\n3\n3","output":"7 2"},{"id":1756972205164,"input":"6 7\n1\n5\n3\n3\n5\n1","output":"2 3"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\AlgoLeadME-15\\AlgoLeadMe-15\\Seol-Munhyeok\\개똥벌레.py","group":"local","local":true} \ No newline at end of file diff --git "a/Seol-Munhyeok/.cph/.\352\260\234\353\230\245\353\262\214\353\240\2102.py_f7b1bfedf84554024f689b69b61e08ce.prob" "b/Seol-Munhyeok/.cph/.\352\260\234\353\230\245\353\262\214\353\240\2102.py_f7b1bfedf84554024f689b69b61e08ce.prob" new file mode 100644 index 0000000..465ebc2 --- /dev/null +++ "b/Seol-Munhyeok/.cph/.\352\260\234\353\230\245\353\262\214\353\240\2102.py_f7b1bfedf84554024f689b69b61e08ce.prob" @@ -0,0 +1 @@ +{"name":"Local: 개똥벌레2","url":"d:\\AlgoLeadME-15\\AlgoLeadMe-15\\Seol-Munhyeok\\개똥벌레2.py","tests":[{"id":1756973855147,"input":"14 5\n1\n3\n4\n2\n2\n4\n3\n4\n3\n3\n3\n2\n3\n3","output":"7 2"},{"id":1756974701757,"input":"6 7\n1\n5\n3\n3\n5\n1","output":"2 3"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\AlgoLeadME-15\\AlgoLeadMe-15\\Seol-Munhyeok\\개똥벌레2.py","group":"local","local":true} \ No newline at end of file diff --git "a/Seol-Munhyeok/.cph/.\354\240\234\352\263\261\354\210\230\354\260\276\352\270\260.py_ea7559ebea594486c87e81b9ee32f04b.prob" "b/Seol-Munhyeok/.cph/.\354\240\234\352\263\261\354\210\230\354\260\276\352\270\260.py_ea7559ebea594486c87e81b9ee32f04b.prob" new file mode 100644 index 0000000..48038c1 --- /dev/null +++ "b/Seol-Munhyeok/.cph/.\354\240\234\352\263\261\354\210\230\354\260\276\352\270\260.py_ea7559ebea594486c87e81b9ee32f04b.prob" @@ -0,0 +1 @@ +{"name":"Local: 제곱수찾기","url":"d:\\AlgoLeadME-15\\AlgoLeadMe-15\\Seol-Munhyeok\\제곱수찾기.py","tests":[{"id":1755608679338,"input":"2 3\n123\n456","output":"64"},{"id":1755610569758,"input":"9 9\n257240281\n197510846\n014345401\n035562575\n974935632\n865865933\n684684987\n768934659\n287493867","output":"95481"},{"id":1755613365657,"input":"1 1\n1","output":""},{"id":1755613442978,"input":"1 2\n12","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\AlgoLeadME-15\\AlgoLeadMe-15\\Seol-Munhyeok\\제곱수찾기.py","group":"local","local":true} \ No newline at end of file diff --git "a/Seol-Munhyeok/.cph/.\354\260\250\353\237\211\353\260\260\354\271\230.py_1ee6dc7200a143d3ff95e315a3e7cb91.prob" "b/Seol-Munhyeok/.cph/.\354\260\250\353\237\211\353\260\260\354\271\230.py_1ee6dc7200a143d3ff95e315a3e7cb91.prob" new file mode 100644 index 0000000..bdbc1c9 --- /dev/null +++ "b/Seol-Munhyeok/.cph/.\354\260\250\353\237\211\353\260\260\354\271\230.py_1ee6dc7200a143d3ff95e315a3e7cb91.prob" @@ -0,0 +1 @@ +{"name":"Local: 차량배치","url":"d:\\AlgoLeadME-15\\AlgoLeadMe-15\\Seol-Munhyeok\\차량배치.py","tests":[{"id":1756981134725,"input":"3 3\n1 2\n1 3\n2 3","output":"5"},{"id":1756981248426,"input":"3 2\n1 2\n2 3","output":"7"},{"id":1756981481893,"input":"6 5\n1 2\n1 3\n2 4\n2 5\n3 6","output":"23"},{"id":1756981635073,"input":"1 0\n","output":"1"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\AlgoLeadME-15\\AlgoLeadMe-15\\Seol-Munhyeok\\차량배치.py","group":"local","local":true} \ No newline at end of file diff --git "a/Seol-Munhyeok/30\353\262\210" "b/Seol-Munhyeok/30\353\262\210" new file mode 100644 index 0000000..4fb7649 --- /dev/null +++ "b/Seol-Munhyeok/30\353\262\210" @@ -0,0 +1,7 @@ +t = int(input()) +for _ in range(t): + a, b = map(int, input().split()) + while a != b: + if a > b: a //= 2 + else: b //= 2 + print(10 * a) \ No newline at end of file diff --git a/Seol-Munhyeok/ATM.py b/Seol-Munhyeok/ATM.py new file mode 100644 index 0000000..1e06690 --- /dev/null +++ b/Seol-Munhyeok/ATM.py @@ -0,0 +1,10 @@ +n = int(input()) +lst = list(map(int, input().split())) + +lst.sort() +p_sum = [0] * (n + 1) + +for i in range(n): + p_sum[i + 1] = p_sum[i] + lst[i] + +print(sum(p_sum)) diff --git a/Seol-Munhyeok/README.md b/Seol-Munhyeok/README.md index cc2a4c4..adea734 100644 --- a/Seol-Munhyeok/README.md +++ b/Seol-Munhyeok/README.md @@ -15,4 +15,8 @@ | 19차시 | 2025.08.10 | 구현, BFS | [거리두기 확인하기](https://school.programmers.co.kr/learn/courses/30/lessons/81302) | https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/67 | | 20차시 | 2025.08.14 | DP | [코딩 테스트 공부](https://school.programmers.co.kr/learn/courses/30/lessons/118668) | https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/73 | AlgoLeadMe/AlgoLeadMe-15/pull/67 | -| 21차시 | 2025.08.18 | 시뮬레이션, 그리디 | [양궁 대회](https://school.programmers.co.kr/learn/courses/30/lessons/92342) | https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/75 | \ No newline at end of file +| 21차시 | 2025.08.18 | 시뮬레이션, 그리디 | [양궁 대회](https://school.programmers.co.kr/learn/courses/30/lessons/92342) | https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/75 | +| 22차시 | 2025.08.23 | 그리디, 투 포인터 | [두 큐 합 같게 만들기](https://school.programmers.co.kr/learn/courses/30/lessons/118667) | https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/79 | +| 23차시 | 2025.09.03 | 수학, 구현 | [책 페이지](https://www.acmicpc.net/problem/1019) | https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/84 | +| 24차시 | 2025.09.03 | 구현 | [수식 최대화](https://school.programmers.co.kr/learn/courses/30/lessons/67257) | https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/85 | +| 25차시 | 2025.09.06 | 문자열, 구현 | [방금그곡](https://school.programmers.co.kr/learn/courses/30/lessons/17683) | https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/86 | diff --git a/Seol-Munhyeok/tempCodeRunnerFile.py b/Seol-Munhyeok/tempCodeRunnerFile.py index aad8b78..d00e753 100644 --- a/Seol-Munhyeok/tempCodeRunnerFile.py +++ b/Seol-Munhyeok/tempCodeRunnerFile.py @@ -1,4 +1 @@ -# 7 6 2 3 15 6 9 8 -# 3 1 1 8 14 7 10 1 -# 6 1 13 6 4 3 11 4 -# 16 1 8 7 5 2 12 2 \ No newline at end of file +down \ No newline at end of file diff --git "a/Seol-Munhyeok/\352\260\234\353\230\245\353\262\214\353\240\210.py" "b/Seol-Munhyeok/\352\260\234\353\230\245\353\262\214\353\240\210.py" new file mode 100644 index 0000000..a9cdfcf --- /dev/null +++ "b/Seol-Munhyeok/\352\260\234\353\230\245\353\262\214\353\240\210.py" @@ -0,0 +1,28 @@ +from bisect import bisect_left + +# 입력 처리 +N, H = map(int, input().split()) +down, up = [], [] +for i in range(N): + h = int(input()) + if i % 2 == 0: + down.append(h) + else: + up.append(h) + +down.sort() +up.sort() + +best = int(1e9) # 파괴해야 하는 장애물의 최솟값 +way = 1 # 구간의 수 +for h in range(1, H + 1): + cnt_down = N // 2 - bisect_left(down, h) + cnt_up = N // 2 - bisect_left(up, H - h + 1) + cnt = cnt_down + cnt_up + if best > cnt: + best = cnt + way = 1 + elif best == cnt: + way += 1 + +print(best, way) diff --git "a/Seol-Munhyeok/\352\260\234\353\230\245\353\262\214\353\240\2102.py" "b/Seol-Munhyeok/\352\260\234\353\230\245\353\262\214\353\240\2102.py" new file mode 100644 index 0000000..f9db7c7 --- /dev/null +++ "b/Seol-Munhyeok/\352\260\234\353\230\245\353\262\214\353\240\2102.py" @@ -0,0 +1,28 @@ +import sys +input = sys.stdin.readline + +N, H = map(int, input().split()) + +# line[i] = 높이 i에서 시작되는 충돌 변화량 +line = [0] * H + +for t in range(N): + h = int(input()) + if t % 2 == 0: # 석순(아래에서 위로 올라옴) + line[0] += 1 + line[h] -= 1 + else: # 종유석(위에서 아래로 내려옴) + line[H - h] += 1 + +# prefix[y] = 높이 y에서 총 충돌 개수 +prefix = [0] * (H + 1) +for i in range(H): + prefix[i + 1] = prefix[i] + line[i] + +prefix = prefix[1:] + +print(line) +print(prefix) +best = min(prefix) +way = prefix.count(best) +print(best, way) diff --git "a/Seol-Munhyeok/\353\221\220\355\201\220\355\225\251\352\260\231\352\262\214\353\247\214\353\223\244\352\270\2601.py" "b/Seol-Munhyeok/\353\221\220\355\201\220\355\225\251\352\260\231\352\262\214\353\247\214\353\223\244\352\270\2601.py" new file mode 100644 index 0000000..160d420 --- /dev/null +++ "b/Seol-Munhyeok/\353\221\220\355\201\220\355\225\251\352\260\231\352\262\214\353\247\214\353\223\244\352\270\2601.py" @@ -0,0 +1,26 @@ +from collections import deque + +def solution(queue1, queue2): + q1, q2 = deque(queue1), deque(queue2) + sum1, sum2 = sum(q1), sum(q2) + + if (sum1 + sum2) % 2 == 1: + return -1 + + n = len(q1) + for i in range(4*n+1): + if sum1 > sum2: + value = q1.popleft() + q2.append(value) + sum1 -= value + sum2 += value + elif sum1 < sum2: + value = q2.popleft() + q1.append(value) + sum2 -= value + sum1 += value + else: + return i + + return -1 + \ No newline at end of file diff --git "a/Seol-Munhyeok/\353\221\220\355\201\220\355\225\251\352\260\231\352\262\214\353\247\214\353\223\244\352\270\2602.py" "b/Seol-Munhyeok/\353\221\220\355\201\220\355\225\251\352\260\231\352\262\214\353\247\214\353\223\244\352\270\2602.py" new file mode 100644 index 0000000..07c1ffa --- /dev/null +++ "b/Seol-Munhyeok/\353\221\220\355\201\220\355\225\251\352\260\231\352\262\214\353\247\214\353\223\244\352\270\2602.py" @@ -0,0 +1,30 @@ +# 투 포인터 풀이 +# https://blog.encrypted.gg/1076 를 참고함 + +def solution(queue1, queue2): + n = len(queue1) + a = queue1 + queue2 + target = sum(a) + if target % 2 != 0: + return -1 + target //= 2 # 합이 target이 되는 구간을 찾아야 함 + + ans = int(1e9) # 일단 매우 큰 값을 넣어놓고 시작 + en = 0 + tot = a[0] # 현재 보는 구간의 합 = a[st]+...+a[en], 범위가 양 끝을 포함(inclusive)한다는 점과 en < st일 땐 a[st]+...+a[2*n-1]+a[0]+...+a[en]을 나타냄에 주의 + for st in range(2*n): + while tot < target: + en = (en + 1) % (2*n) + tot += a[en] + + # while문을 탈출하면 a[st]+...+a[en] 구간의 합이 target 이상임을 의미 + if tot == target: # 구간의 합이 정확히 target과 일치할 경우 + moves = 0 # 작업 횟수 + if en < n-1: moves = 3*n + 1 + st + en # 3n+1번의 연산을 통해 큐1에 첫 번째 원소만 남길 수 있고, 이후 st+en 번의 추가적인 연산을 거쳐 a[st] .. a[en]을 큐1에 둘 수 있음 + else: moves = st + (en - n + 1) + ans = min(ans, moves) + + tot -= a[st] + + if ans == int(1e9): ans = -1 + return ans \ No newline at end of file diff --git "a/Seol-Munhyeok/\353\247\244\354\232\260\354\226\264\353\240\244\354\232\264\353\254\270\354\240\234.py" "b/Seol-Munhyeok/\353\247\244\354\232\260\354\226\264\353\240\244\354\232\264\353\254\270\354\240\234.py" new file mode 100644 index 0000000..a3fde68 --- /dev/null +++ "b/Seol-Munhyeok/\353\247\244\354\232\260\354\226\264\353\240\244\354\232\264\353\254\270\354\240\234.py" @@ -0,0 +1,12 @@ +n, m = map(int, input().split()) + +def solution(n, m): + if n > m: + return 0 + tmp = 1 + for i in range(2, n+1): + tmp = (tmp * i) % m + return tmp + +print(solution(n, m)) + diff --git "a/Seol-Munhyeok/\353\260\251\352\270\210\352\267\270\352\263\241.py" "b/Seol-Munhyeok/\353\260\251\352\270\210\352\267\270\352\263\241.py" new file mode 100644 index 0000000..04ca03e --- /dev/null +++ "b/Seol-Munhyeok/\353\260\251\352\270\210\352\267\270\352\263\241.py" @@ -0,0 +1,32 @@ +def time_to_sec(time): + h, m = time.split(":") + return 60 * int(h) + int(m) + +def convert(score): + lst = [("B#", "b"), ("C#", "c"), ("D#", "d"), ("E#", "e"), ("F#", "f"), ("G#", "g"), ("A#", "a")] + for a, b in lst: + score = score.replace(a, b) + return score + +def convert_2(score, length): + size = len(score) + if size == length: + return score + if size > length: + return score[:length] + if size < length: + return score * (length // size) + score[:length % size] + + +def solution(m, musicinfos): + answer = '(None)' + max_size = -1 + for info in musicinfos: + start, end, title, score = info.split(",") + length = time_to_sec(end) - time_to_sec(start) + m, score = convert(m), convert(score) + if m in convert_2(score, length) and length > max_size: + answer = title + max_size = length + + return answer \ No newline at end of file diff --git "a/Seol-Munhyeok/\354\210\230\354\213\235\354\265\234\353\214\200\355\231\224.py" "b/Seol-Munhyeok/\354\210\230\354\213\235\354\265\234\353\214\200\355\231\224.py" new file mode 100644 index 0000000..bd1b103 --- /dev/null +++ "b/Seol-Munhyeok/\354\210\230\354\213\235\354\265\234\353\214\200\355\231\224.py" @@ -0,0 +1,43 @@ +from itertools import permutations + +def solution(expression): + # 토큰 분리 + token = [] + operators = set() + num = '' + for e in expression: + if e.isdigit(): + num += e + else: + token.append(int(num)) + token.append(e) + operators.add(e) + num = '' + token.append(int(num)) + + max_val = -1 + # 모든 우선 순위 고려 + for ops in permutations(operators): + temp = token[:] + # 연산자 우선 순위대로 처리 + for op in ops: + stack = [] + i = 0 + while i < len(temp): + if temp[i] == op: + val_1 = stack.pop() + val_2 = temp[i + 1] + if op == '+': + stack.append(val_1 + val_2) + elif op == '-': + stack.append(val_1 - val_2) + elif op == '*': + stack.append(val_1 * val_2) + i += 2 + else: + stack.append(temp[i]) + i += 1 + temp = stack + max_val = max(max_val, abs(temp[0])) + + return max_val \ No newline at end of file diff --git "a/Seol-Munhyeok/\354\240\234\352\263\261\354\210\230\354\260\276\352\270\260.py" "b/Seol-Munhyeok/\354\240\234\352\263\261\354\210\230\354\260\276\352\270\260.py" new file mode 100644 index 0000000..fefb98c --- /dev/null +++ "b/Seol-Munhyeok/\354\240\234\352\263\261\354\210\230\354\260\276\352\270\260.py" @@ -0,0 +1,33 @@ +import math + +answer = -1 + +def is_square(n): + return math.isqrt(n) ** 2 == n + +def brute(): + global answer + if n == 1 and m == 1: + return A[0][0] if is_square(int(A[0][0])) else -1 + + for r0 in range(n): + for c0 in range(m): + for dr in range(-(n-1), n): + for dc in range(-(m-1), m): + if dr == 0 and dc == 0: + continue + r, c = r0, c0 + num = "" + while 0 <= r < n and 0 <= c < m: + num += A[r][c] + val = int(num) + if is_square(val): + answer = max(answer, val) + r += dr + c += dc + return answer + +n, m = map(int, input().split()) +A = [list(input()) for _ in range(n)] + +print(brute()) \ No newline at end of file diff --git "a/Seol-Munhyeok/\354\260\250\353\237\211\353\260\260\354\271\230.py" "b/Seol-Munhyeok/\354\260\250\353\237\211\353\260\260\354\271\230.py" new file mode 100644 index 0000000..44d7df0 --- /dev/null +++ "b/Seol-Munhyeok/\354\260\250\353\237\211\353\260\260\354\271\230.py" @@ -0,0 +1,28 @@ +from collections import defaultdict, deque +import sys + +input = sys.stdin.readline +N, M = map(int, input().split()) +graph = defaultdict(list) +for _ in range(M): + a, b = map(int, input().split()) + graph[a].append(b) + graph[b].append(a) + +visited = [False] * (N+1) +queue = deque() +queue.append(1) +visited[1] = True +answer = 1 + +while queue: + size = len(queue) + answer *= (size + 1) + for _ in range(size): + cur = queue.popleft() + for neighbor in graph[cur]: + if not visited[neighbor]: + queue.append(neighbor) + visited[neighbor] = True + +print((answer - 1) % 1_000_000_007) \ No newline at end of file diff --git "a/Seol-Munhyeok/\354\261\205 \355\216\230\354\235\264\354\247\200.py" "b/Seol-Munhyeok/\354\261\205 \355\216\230\354\235\264\354\247\200.py" new file mode 100644 index 0000000..b8a0ba1 --- /dev/null +++ "b/Seol-Munhyeok/\354\261\205 \355\216\230\354\235\264\354\247\200.py" @@ -0,0 +1,48 @@ +answer = [0] * 10 # 0~9 각 숫자의 총 등장 횟수를 담을 배열 + +start = 1 # 왼쪽 포인터: 아직 세지 않은 최소 숫자 +end = int(input()) # 오른쪽 포인터: 아직 세지 않은 최대 숫자(입력 N) +ten = 1 # 현재 바라보는 자릿값(일=1, 십=10, 백=100, ...) + +# 숫자 n이 지금 바라보는 자릿값(ten=1,10,100,...)에서 +# '접두사'로서 기여하는 고정 등장 횟수를 answer에 더한다. +# 예를들어 calc(12345, 1)은 answer[5], answer[4], ... answer[1]에 +1 +# +ten을 하는 이유는 현재 자리 아래쪽에는 항상 ten가지(0..ten-1)의 조합이 깔리므로, +# 그 자리의 같은 숫자 d가 블록 안에서 정확히 ten번 등장하기 때문. +def calc(n, ten): + while n > 0: + answer[n % 10] += ten + n //= 10 + +# 바깥 while 한 바퀴 = 자릿값 'ten' 하나를 완전히 처리 +while start <= end: + # [1] 왼쪽 모서리 정리: start를 '...0'으로 맞출 때까지 개별 처리 + while start % 10 != 0 and start <= end: + calc(start, ten) + start += 1 + if start > end: break # 다 끝나면 종료 + + # [2] 오른쪽 모서리 정리: end를 '...9'로 맞출 때까지 개별 처리 + while end % 10 != 9 and start <= end: + calc(end, ten) + end -= 1 + + # 여기까지 오면 [start, end]는 현재 자리에서 + # 정확히 '...0 ~ ...9'가 반복되는 '완전 주기 구간'이 됨. + + # [3] 가운데 완전 주기 한 번에 더하기 + # cnt = 현재 자리에서 서로 다른 '접두사' 개수 + # (접두사 하나마다 0~9 각 숫자가 하위 ten가지와 결합하므로 + # 각 숫자가 정확히 'ten'번씩 등장) + cnt = (end // 10) - (start) // 10 + 1 + for d in range(0, 10): + answer[d] += cnt * ten + + # [4] 한 자리 올림: 현재 자리 처리를 끝냈으니 윗자리로 이동 + start //= 10 + end //= 10 + ten *= 10 + +# 결과 출력 +for d in range(0, 10): + print(answer[d], end=' ') diff --git "a/Seol-Munhyeok/\355\206\240\353\247\210\355\206\240.py" "b/Seol-Munhyeok/\355\206\240\353\247\210\355\206\240.py" new file mode 100644 index 0000000..3fcf980 --- /dev/null +++ "b/Seol-Munhyeok/\355\206\240\353\247\210\355\206\240.py" @@ -0,0 +1,37 @@ +from collections import deque +import sys + +m, n = map(int, input().split()) +arr = [] +dy, dx = [-1, 0, 1, 0], [0, 1, 0, -1] +queue = deque() + +# 입력 처리, 1인 좌표 저장 +for i in range(n): + row = [] + for j, val in enumerate(map(int, input().split())): + row.append(val) + if val == 1: + queue.append((i, j)) + arr.append(row) + +# BFS +while queue: + y, x = queue.popleft() + for i in range(4): + ny, nx = y + dy[i], x + dx[i] + if 0 <= ny < n and 0 <= nx < m and arr[ny][nx] == 0: + arr[ny][nx] = arr[y][x] + 1 + queue.append((ny, nx)) + +# 정답 찾기 +answer = -1e9 +for i in range(n): + for j in range(m): + if arr[i][j] == 0: + print(-1) + sys.exit() + if arr[i][j] > answer: + answer = arr[i][j] + +print(answer - 1)