Towers of Hanoi with “counter” in python

喜夏-厌秋 提交于 2019-12-24 01:54:21

问题


I have written a code for "Towers of Hanoi" in python and I am trying to add a counter to show how many times it has run. I tried several things like a while loop and for loops etc. but it doesn't work. I am sure that the answer is pretty easy but my brain is running on the lowest setting right now. My code looks like this:

def Hanoi(n, src, dst, tmp):
if n > 0:
    Hanoi(n - 1, src, tmp, dst)
    print "Move disc", chr(64 + n), "From tower", src, "to tower", dst
    Hanoi(n - 1, tmp, dst, src)

Hanoi(4,0,2,1) #Just an example

I know that the code has to run (2^n)-1 times, but I just cant implement it. Because the way I wrote the code the value n changes so that would work for me. (I tried something like this:

def Hanoi(n, src, dst, tmp):

    a = 0
    while (a < (2**n)-1)
        a+=1

    if n > 0:
        Hanoi(n - 1, src, tmp, dst)
        print a, "Move disc", chr(64 + n), "From tower", src, "to tower", dst
        Hanoi(n - 1, tmp, dst, src)

but as I've said, the value n changes and I don't know how to fix it.

Edit:

To clarify, I want it to shop the number of steps like this: (If I call Hanoi(3,0,2,1))

1. Move disc A From tower 0 to tower 2
2. Move disc B From tower 0 to tower 1
3. Move disc A From tower 2 to tower 1
4. Move disc C From tower 0 to tower 2
5. Move disc A From tower 1 to tower 0
6. Move disc B From tower 1 to tower 2
7. Move disc A From tower 0 to tower 2

回答1:


How about returning the number of calls from your function:

def Hanoi(n, src, dst, tmp):
    if n > 0:
        pre = Hanoi(n - 1, src, tmp, dst)
        print "Move disc", chr(64 + n), "From tower", src, "to tower", dst
        post = Hanoi(n - 1, tmp, dst, src)
        return pre + post + 1
    else:
        return 1

Note that this counts the number of calls to the Hanoi function, not the number of moves that actually need to be made if you were physically playing the game. If you want the number of moves, just change the return 1 in the base case (the last line) to return 0.




回答2:


You can use a global counter to keep track of how many time the function is called.

runcount = 0 

def Hanoi(n, src, dst, tmp):

    if n > 0:
        global runcount
        runcount += 1 
        Hanoi(n - 1, src, tmp, dst)
        print "Move disc", chr(64 + n), "From tower", src, "to tower", dst 
        Hanoi(n - 1, tmp, dst, src)

Hanoi(4,0,2,1) #Just an example

print runcount

It prints 15 at the end.




回答3:


1.def hanoi(n,s,t,b):

   assert n > 0
    if n == 1:
        print 'move',s,'to',t
        return 1     #######count number of iteration
    else:
        first=hanoi(n-1,s,b,t)
        second=hanoi(1,s,t,b)
        third=hanoi(n-1,b,t,s)
        return first+second+third#####return times of 1
    ###print it on calling

for i in range(1,5):

    print 'New Hanoi Example: hanoi(',i,',source, target, buffer)'
    print '----------------------'
    print hanoi(i,'source','target','buffer')


来源:https://stackoverflow.com/questions/21876214/towers-of-hanoi-with-counter-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!