find the lowest number location

before

#设定路径列表Path
def find_path2(heightmap, x, y, water_level=557,path=[]):
    #global path
    #设定坐标 右0 左1 上2 下3   
    right_point=heightmap[x][y+1]
    left_point=heightmap[x][y-1]
    above_point=heightmap[x-1][y]
    below_point=heightmap[x+1][y]
    #zip[*]
    go_path=[right_point,left_point,above_point,below_point]
    go_path1 = ['right','left','above',' Below ' ]
     # Circle log     
    Print ( " current point value:% d Min index:% s minimum of:% d " 
          % (the heightmap [X] [Y], go_path1 [go_path.index (min ( go_path))], 
            go_path [go_path.index (min (go_path))])) 
    IF min (go_path)> water_level:
         IF min (go_path) <= the heightmap [X] [Y]:
             IF go_path.index (min (go_path )) == 0: # right 
                path.append ((X, Y +. 1 )) 
                find_path2 (the heightmap, X, Y + 1'd, water_level = water_level)
             IF go_path.index(min(go_path)) == 1: #left
                path.append((x,y-1))
                find_path2(heightmap, x, y-1, water_level=water_level)
            if go_path.index(min(go_path)) == 2: #above
                path.append((x-1,y))
                find_path2(heightmap, x-1, y, water_level=water_level)
            if go_path.index(min(go_path)) == 3: #below
                path.append((x+1,y))
                find_path2(heightmap, x+1, y, water_level=water_level)
        return path
    return path

def sun(heightmap, x, y, water_level=0):    
    way = []
    way.clear()
    way.append(find_path2(heightmap, x, y, water_level=water_level))
    return way


find_path2(test,0,0,water_level=0)
sun(test,1,0,water_level=0)


test = ((10,10,10,10)
        ,(10,9,8,10)
        ,(10,10,3,10)
        ,(10,10,10,10)
        )

the question is that

find_path2 calls itself recursively, path does not release local variables content 

after

def find_path3(heightmap, x, y,go_path):
    #global path
    #设定坐标 右0 左1 上2 下3   
    right_point=heightmap[x][y+1]
    left_point=heightmap[x][y-1]
    above_point=heightmap[x-1][y]
    below_point=heightmap[x+1][y]
    #zip[*]
    go_path=[right_point,left_point,above_point,below_point]
    go_path1 = ['right','left','above','below']
    #circle log    
    print("现在点的值为:%d 最小值为索引:%s 最小值为:%d"
          %(heightmap[x][y],go_path1[go_path.index(min(go_path))],
            go_path[go_path.index(min(go_path))]))
    if min(go_path) > water_level:
        if min(go_path) <= heightmap[x][y]:
            if go_path.index(min(go_path)) == 0: #right
                return(x,y+1)
                #path.append((x,y+1))
                #find_path2(heightmap, x, y+1, water_level=water_level)
            if go_path.index(min(go_path)) == 1: #left
                return(x,y-1)
                #path.append((x,y-1))
                #find_path2(heightmap, x, y-1, water_level=water_level)
            if go_path.index(min(go_path)) == 2: #above
                return(x-1,y)
                #path.append((x-1,y))
                #find_path2(heightmap, x-1, y, water_level=water_level)
            if go_path.index(min(go_path)) == 3: #below
                return(x+1,y)
                #path.append((x+1,y))
                #find_path2(heightmap, x+1, y, water_level=water_level)
    return None
    print("the current number is less then the water_level")


def route(heightmap, x, y, water_level=0):
    route = []
    next_step = find_path3(heightmap, x, y,go_path)
    while next_step:
        route.append(next_step)
        x,y = next_step
        next_step = find_path3(heightmap, x, y,go_path)
    return route

test = ((10,10,10,10)
        ,(10,9,8,10)
        ,(10,10,3,10)
        ,(10,10,10,10)
        )
        
find_path3(test,0,0,water_level=1)
route(test,0,0,water_level=0)

Out step by step, not after the call itself, route variable is first set to empty



Guess you like

Origin www.cnblogs.com/pingzideping/p/10936120.html