food = 5 grid = [[0, 2, 5], [1, 1, 3], [2, 1, 1]] n = len(grid) - 1 rooms = n + n last_zombie = grid[n][n] results = [] results.append(food) print "Array row %s, col %s" % (n,n) def enter(x, y, remaining_food): #check all four adjacent cells # if results[0] == 0: # return if x == y and y == n: print remaining_food if remaining_food < results[0]: results[0] = remaining_food return print "Checking row %s, column %s on %s with %s food" % (x,y, grid, remaining_food) zombie_hunger = grid[x][y] if zombie_hunger <= remaining_food: remaining_food -= zombie_hunger # Last Room Condition # end of path conditions rooms_remaining = rooms - x - y - 1 if results[0] != 0 and remaining_food >= rooms_remaining: if x < n: enter(x + 1, y, remaining_food) if y < n and results[0] != 0: enter(x, y + 1, remaining_food) else: print "Ending Early" return enter(0, 0, food-last_zombie) print results[0]
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question