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变量首先置为空了



转载于:https://www.cnblogs.com/pingzideping/p/10936120.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值