所以我試圖創建一個洪水填充算法,我不斷收到與此遞歸錯誤。該算法似乎有無限遞歸,我不能明確原因。根據大多數消息來源,我看遍了互聯網,找不到解決方案,因爲看起來我的程序是正確的。但是,似乎有什麼問題。這是代碼的編輯版本。錯誤消息仍然是最大遞歸。洪水填充算法Python
我可以得到一些幫助嗎?
from Tkinter import *
from PIL import Image, ImageTk
from random import *
w= 75
h= w
flood = Image.new("RGB", (w,h), (0,0,0))
x = 0
y = 0
count = 0
colorlist = []
i = 0
while x < w -1:
y = 0
while y < h-1:
r = random()
if r < .25:
flood.putpixel((x,y), (0,0,0))
else:
flood.putpixel((x,y), (255,255,255))
y += 1
x += 1
x = 0
y = 0
while x < w-1:
y = 0
while y < h-1:
r = random()
if x == 0 or y == 0 or x == w-1 or y ==h-1:
flood.putpixel((x,y), (0,0,0))
y += 1
x += 1
def floodfill(x,y, d,e,f, g,h,i, image, count):
count+=1
(a,b,c) = image.getpixel((x,y))
if (a,b,c) == (255,255,255):
(j,k,l) = image.getpixel((x-1,y))
(m,n,o) = image.getpixel((x+1, y))
(p,q,r) = image.getpixel((x,y-1))
(s,t,u) = image.getpixel((x,y+1))
if count > 990:
return
if (a,b,c) == (255,255,255):
image.putpixel((x,y), (g,h,i))
floodfill(x-1, y, d,e,f, g,h,i, image, count)
floodfill(x+1, y, d,e,f, g,h,i, image, count)
floodfill(x, y-1, d,e,f, g,h,i, image, count)
floodfill(x, y+1, d,e,f, g,h,i, image,count)
floodfill(2,2, 0,0,0,255,0,0,flood, 0)
flood.save("flood.png")
print "done"
請提供您正在查看的確切錯誤消息。 – 2012-07-31 18:43:44
RuntimeError:在調用Python對象時超出最大遞歸深度 – user1541756 2012-07-31 18:49:17
您似乎沒有檢查x,y是否超出範圍 - 這可能導致無限遞歸,因爲x快速左移。或者你是否希望你的邊界照顧到這一點? – user1277476 2012-07-31 18:54:50