$cache={} def robot(width,height,x=0,y=0) return 1 if x == width-1 and y == height-1 # We reached the Finish, 1 solution found return $cache[[width,height,x,y]] if $cache[[width,height,x,y]] # Did we already computed the solution for this square? Just return it instead of recompute. solutions = 0 solutions += robot(width,height,x+1,y) if x != width-1 # The number of solutions starting from the current square (x,y) are all the solutions starting at x+1,y (if it is valid to move on the right) solutions += robot(width,height,x,y+1) if y != height-1 # Plus the solutions starting at x,y+1 (if it is valid to move on the bottom) $cache[[width,height,x,y]] = solutions # Save the solution for this square return solutions end puts(robot(31,43))