python bfs
whats the cost of path p \n\npath p == [u,v,w,x,y,z]\n\n\nits the cumulative total cost\n+ cost to get from u to v \n+ cost to get from v to w \n+ cost to get f
whats the cost of path p \n```\npath p == [u,v,w,x,y,z]\n```\n\nits the cumulative total cost\n+ cost to get from u to v \n+ cost to get from v to w \n+ cost to get from w to x .\n+ etc...\n\nthe order we want is from smallest tree to highest\nthe nodes are actually coordinates, and their value in grid is used to rank them\n```\ns = [(i,j) for i in range(len(f)) for j in range(len(f[0])) if f[i][j]]                \ns.sort(key = lambda x: f[x[0]][x[1]])\n```\nand we always start by (0,0)\n```\ns = [(0,0)] + s\n```\ncumul is the answer\n```\ncumul = 0\nfor u,v in zip(s,s[1:]): \n\tcumul += self.bfs(u,v,f)\n\tif cumul == float(\'inf\'): return -1\nreturn cumul\n```\n\nyou can walk passed uncut tree if, I was checking that the height would be strictly increasing from (0,0), but you can actually walk through a tree\nthe only forbidden cell is the zero\n\n\n\n\ncode\n```\nclass Solution(object):\n    def cutOffTree(self, f):\n        s = [(i,j) for i in range(len(f)) for j in range(len(f[0])) if f[i][j]]                \n        s.sort(key = lambda x: f[x[0]][x[1]])\n        s = [(0,0)] + s\n        cumul = 0\n        for u,v in zip(s,s[1:]): \n            cumul += self.bfs(u,v,f)\n            if cumul == float(\'inf\'): return -1\n        return cumul\n    \n    def bfs(self,source,target,grid):\n        q,vis = collections.deque([(source,0)]),set([source])\n        while(q):\n            u,steps  = q.popleft()\n            if u == target: return steps\n            for v in [(u[0]-1,u[1]), (u[0]+1,u[1]), (u[0],u[1]-1), (u[0],u[1]+1)]:\n                if 0 <= v[0] < len(grid) and 0 <= v[1] < len(grid[0]) and grid[v[0]][v[1]] != 0 and v not in vis:\n                    vis.add(v)\n                    q.append((v,steps + 1))\n        return +float(\'inf\')\n```
['Breadth-First Search', 'Python']