def answer(food,grid): from sets import Set foodGrid = [] col_cost_to_get_here = set([]) n = len(grid) for row in range(n): # foodGrid[row][0]=[0] foodGrid.append([]) #if row > 1: # foodGrid[row - 2] = set([]) for col in range(n): foodGrid[row].append([]) row_cost_to_get_here = set([]) if col == 0 and row == 0: foodGrid[0][0].append(set([0])) else : print "Array row %s, col %s" % (row,col) print foodGrid # print foodGrid[1][0] zombie_hunger = grid[row][col] if row != 0: print "Calculating Row" row_cost_to_get_here.update([x+zombie_hunger for x in foodGrid[row-1][col][0]]) print "Cost to get here %s" % row_cost_to_get_here if col != 0: print "Calculating Column %s " % foodGrid[row][col-1][0] row_cost_to_get_here.update([x+zombie_hunger for x in foodGrid[row][col-1][0]]) print col_cost_to_get_here foodGrid[row][col].append(row_cost_to_get_here) #foodGrid[n-1][n-1][0].sort() paths = sorted(foodGrid[n-1][n-1][0]) print foodGrid from bisect import bisect return max(food - paths[bisect(paths, food) - 1], -1) food = 12 grid = [[0, 2, 5], [1, 1, 3], [2, 1, 1]] #rooms = n + n #last_zombie = grid[n][n] #results = [] #results.append(food) #print "Array row %s, col %s" % (n,n) print answer(food, grid)
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