tomatobox = []
findlist = []
countday = 0
f = 0
r = 0
xnum, ynum = map(int, input().split())
x = [-1]*(xnum+2)
tomatobox.append(x)
for i in range(ynum): # 토마토 박스 만들귀
a = []
a.append(-1)
data = list(map(int, input().split()))
for x in range(len(data)):
a.append(data[x])
a.append(-1)
tomatobox.append(a)
x = [-1]*(xnum+2)
tomatobox.append(x)
def findone(): # 숫자 1의 좌표값 찾기
global findlist
global f
findlist = []
for y in range(1, ynum+2):
for x in range(1, xnum+2):
if tomatobox[y][x] == 1:
onenum = [y, x]
findlist.append(onenum)
def findunripe(num):
global f
x = num[0]
y = num[1]
# print(tomatobox[x+1][y])
# print(tomatobox[x-1][y])
# print(tomatobox[x][y+1])
# print(tomatobox[x][y-1])
if (tomatobox[x+1][y] == 0):
tomatobox[x+1][y] = 1
findlist.append([x+1, y])
f += 1
if (tomatobox[x-1][y] == 0):
tomatobox[x-1][y] = 1
findlist.append([x-1, y])
f += 1
if (tomatobox[x][y+1] == 0):
tomatobox[x][y+1] = 1
findlist.append([x, y+1])
f += 1
if (tomatobox[x][y-1] == 0):
tomatobox[x][y-1] = 1
findlist.append([x, y-1])
f += 1
def ripe():
global countday
global r
forcount = 0
while True:
if findlist.count(0) == len(findlist):
break
else:
for i in findlist:
if forcount == 0:
forcount = len(findlist) - findlist.count(0)
countday += 1
forcount -= 1
findunripe(i)
findlist[r] = 0
r += 1
# print(countday-1)
def cantripe():
for y in range(1, ynum + 2):
for x in range(1, xnum + 2):
if tomatobox[y][x] == 0:
return True
isallripe = False
def allripe():
global isallripe
for y in range(1, ynum + 2):
for x in range(1, xnum + 2):
if tomatobox[y][x] == 1 or tomatobox[y][x] == -1:
isallripe = True
else:
return False
return isallripe
findone()
if allripe():
print(0)
else:
ripe()
if cantripe():
print(-1)
else:
print(countday-1)
top of page
기능을 테스트하려면 라이브 사이트로 이동하세요.
211009
211009
댓글 0개
좋아요
댓글(0)
bottom of page