문제
https://www.acmicpc.net/problem/2638
풀이
python3
import sys
sys.setrecursionlimit((10**6))
# 좌우상하
dx = [-1,1,0,0]
dy = [0,0,1,-1]
def solution(x, y):
for i in range(4):
now_x = x + dx[i]
now_y = y + dy[i]
if 0 <= now_y < n and 0 <= now_x < m and not visit[now_y][now_x]:
if cheese_list[now_y][now_x] != 0:
cheese_list[now_y][now_x] += 1
else:
visit[now_y][now_x] = 1
solution(now_x, now_y)
def melting_cheese():
for i in range(n):
for j in range(m):
if cheese_list[i][j] != 0:
return False
return True
if __name__ == '__main__':
global n, m
n, m = map(int, input().split()) # n : 행 y, m: 열 x
global cheese_list
cheese_list = list()
for _ in range(n):
cheese_list.append(list(map(int, input().split())))
hours = 0
while True:
if melting_cheese():
print(hours)
break
global visit
visit = [[0 for _ in range(m)] for _ in range(n)]
solution(0, 0)
for i in range(n):
for j in range(m):
if cheese_list[i][j] >= 3:
cheese_list[i][j] = 0
elif cheese_list[i][j] > 0:
cheese_list[i][j] = 1
hours += 1
아쉬웠던점
'algorithm > exercise' 카테고리의 다른 글
[BOJ 1963] 백준 1963 소수경로 (0) | 2021.08.30 |
---|---|
[BOJ 2263] 백준 2263 트리의 순회 (0) | 2021.08.29 |
[BOJ 6593] 백준 6593 상범 빌딩 (0) | 2021.08.28 |
[BOJ 14891] 백준 14891 톱니바퀴 (0) | 2021.08.21 |
[BOJ 1937] 백준 1937 욕심쟁이 판다 (0) | 2021.08.19 |