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