def answer_nonrec food grid result 10000 len grid for downs in combina

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def answer_nonrec(food, grid):
result = 10000
N = len(grid)
for downs in combinations(range(2 * N - 2), N - 1):
downs = set(downs)
x = 0
y = 0
food_current = food
eaten = False
for i in xrange(2 * N - 2):
if i in downs:
y += 1
else:
x += 1
food_current -= grid[x][y]
if food_current < 0:
eaten = True
break
if not eaten:
result = min(food_current, result)
return -1 if result == 10000 else result