我有一个带有一个图像(PNG,200x200)的递归迷宫解算器,我曾尝试使用try/except打印异常,但没有打印
递归函数
def solveRecursiveMaze(arr,x,y):
successful = False
if (x,y) == getExitPoint():
successful = True
elif isValid(arr,x,y):
arr[x][y] = "V" #set to V to show it's a visited path
successful = solveRecursiveMaze(arr, x-1, y)
if not successful:
successful = solveRecursiveMaze(arr, x, y+1)
if not successful:
successful = solveRecursiveMaze(arr, x+1, y)
if not successful:
successful = solveRecursiveMaze(arr, x, y-1)
if successful:
arr[x][y] = "P" #Mark as P to show it's a valid pa
return successful
isValid检查2D数组在传入的位置是否有一个“W”(表示白色像素),并且它是否在数组的边界内
def isValid(arr,x,y):
if x < len(arr) and y < len(arr) and x >= 0 and y >= 0:
if arr[x][y] == "W":
return True
return False
getExitPoint返回为迷宫出口找到的点的x,y(像素)
def getExitPoint():
x = crop.size[0] - 1
for y in range(0, crop.size[1]):
if(crop.getpixel((x,y)) == (255,255,255)):
return x,y
if(crop.getpixel((y,x)) == (255,255,255)):
return y,x
这就是我如何将图像转换为二维阵列的方法
maze = []
width,height = crop.size
for x in range(0, width):
mazeX = []
for y in range(0, height):
if(crop.getpixel((x,y)) == (0,0,0)):
mazeX.append("B")
elif(crop.getpixel((x,y)) == (255,255,255)):
mazeX.append("W")
maze.append(mazeX)
脚本在这幅图像上不起作用,每次它都在像素X=76,y=153处停止,我不确定该做什么/我做错了什么
边界为1px,路径为1px。没有任何错误、堆栈跟踪、异常或抛出的任何内容。递归停止,程序退出。有什么想法吗
您的代码存在一些不同的问题,但我认为现在发生的是,您的运行超出了递归限制。你的迷宫相当大和复杂。我需要使用
4000
(对我来说,1000
是默认值,而3000
不够大)不确定您正在使用的图像库;我用了PIL.Image
您的输入图像实际上是203x203,要找到入口和出口是一个挑战。我假设入口在顶部或左侧,出口在右侧或沿底部
相关问题 更多 >
编程相关推荐