diff --git "a/alstjr7437/BFS/\355\206\240\353\247\210\355\206\240.py" "b/alstjr7437/BFS/\355\206\240\353\247\210\355\206\240.py" new file mode 100644 index 00000000..45c32e7b --- /dev/null +++ "b/alstjr7437/BFS/\355\206\240\353\247\210\355\206\240.py" @@ -0,0 +1,40 @@ +from collections import deque +import sys + +input = sys.stdin.readline + +dx = [0,0,-1,1] +dy = [1,-1,0,0] + +n, m = map(int, input().split()) +tomato = [] +visited = [[0] * n for _ in range(m)] + +for _ in range(m): + tomato.append(list(map(int, input().split()))) + +queue = deque() + +for y in range(m): + for x in range(n): + if tomato[y][x] == 1: + queue.append((y,x)) + visited[y][x] = 1 + +while queue: + y, x = queue.popleft() + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if nx >= n or nx < 0 or ny >= m or ny < 0: + continue + if tomato[ny][nx] != -1 and visited[ny][nx] == 0: + visited[ny][nx] = 1 + tomato[ny][nx] = tomato[y][x] + 1 + queue.append((ny, nx)) + +if any(0 in l for l in tomato): + print(-1) +else : + test = max(map(max, tomato)) + print(test - 1) diff --git a/alstjr7437/README.md b/alstjr7437/README.md index 253921c0..c132ba86 100644 --- a/alstjr7437/README.md +++ b/alstjr7437/README.md @@ -24,4 +24,5 @@ | 22차시 | 2024.04.13 | BFS | 연결 요소의 개수 | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/78 | | 21차시 | 2024.04.06 | 비트마스킹 | 집합 | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/74 | | 22차시 | 2024.04.13 | BFS | 연결 요소의 개수 | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/78 | -| 23차시 | 2024.05.01 | 큐 | 프로세스 | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/79 | \ No newline at end of file +| 23차시 | 2024.05.01 | 큐 | 프로세스 | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/79 | +| 24차시 | 2024.05.14 | BFS | 토마토 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/187 | \ No newline at end of file