在經典的切割棒動態規劃問題中 - 在哪裏可以切割長度爲n的棒來最大化我通過出售全杆或其部分。詳情請參閱此處的問題陳述 - Understanding the bottom-up rod cut implementation動態規劃 - 棒切割,同時保留製表切割的指標
如何保留指標的選項卡 - 即在原始棒的哪個位置進行切割? 我對於知道如何保持杆最終被切割的指標似乎沒有公平的理解。 這裏是我的代碼 - :
# price is dict of revenue we get for the corresponding rod-length
price={1:1,2:5,3:8,4:9,5:10,6:17,7:17,8:20,9:24,10:30}
def cut_rod(n,price):
if n==0:
return 0
revenue=-999
for i in range(1,n+1):
##I do this, since my price[1] ie the keys to the price dictionary start from 1...n
revenue=max(revenue,price[i]+cut_rod(n-i,price))
return revenue
n=4 # print the maximum revenue we earn for rod of length 4
revenue=cut_rod(n,price)
print "revenue for rod of length %d is %d" %(n,revenue)
對於長度爲4杆,我就2和2(即中途)的削減 - 我們如何記住這些指標?
編輯:我有一個大致的想法,我需要保持所有收入的標籤到目前爲止,與相應的切割位置 - 但我似乎迷失在主要通話 - 我應該存儲這之間的隨後在dict結構中調用,稍後再查看它。 我也認爲索引將是一個字典(稍後查找)與「列表」作爲值,因爲對於棒長度的一些組合 - 我們可能會減少不止一次。
+1爲了直觀的解釋,你應該回答。
這個問題似乎是題外話,因爲它是關於數學。這是一個編程站點。 – Ali
您可能在http://math.stackexchange.com/或其兄弟姐妹中有更好的運氣。 – Ali