zoukankan      html  css  js  c++  java
  • 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("现在点的值为:%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
                    path.append((x,y+1))
                    find_path2(heightmap, x, y+1, 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)
            )

    问题在于

    find_path2递归调用自身,path局部变量没有释放内容

    after

    
    

    #设定路径列表Path
    def find_path3(heightmap, x, y,water_level):
    #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,water_level)
    while next_step:
    route.append(next_step)
    x,y = next_step
    next_step = find_path3(heightmap, x, y,water_level)
    return route

    
    

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

    find_path3(test,2,2,water_level=1)
    route(test,2,2,water_level=0)

    分步骤进行,不用调用自身后,route变量首先置为空了



  • 相关阅读:
    Logistic Regression
    Bootstrap研究2布局系统杂记
    《Programming in Scala》读书笔记(持续更新) passover的个人空间 DOIT博客 多易网
    《Scala, Erlang, F#作者讨论函数式语言》有感
    用python抓取oj题目(0)——重回战场 duoduo3_69 博客园
    Netty vs Apache MINA
    What is Akka?
    Fault Tolerance (Scala) — Akka Documentation
    execute phase · mrdon/mavencliplugin Wiki
    netty和mina的比较
  • 原文地址:https://www.cnblogs.com/pingzideping/p/10936120.html
Copyright © 2011-2022 走看看