n:有n个铁盘

from:铁盘一开始所在的铁柱

by:铁盘会经过的铁柱

to:铁盘最后到达的铁柱

 

n-1个铁盘:从from到by,to是中途经过的

第n个铁盘:从from到to

n-1个铁盘:从by到to,from是中途经过的

 

import unittest

class TestCase(unittest.TestCase):

    def Hanoi(self,n,fr,by,to):

        if n == 1:
            to.insert(0,fr.pop(0))
        else:
            self.Hanoi(n-1,fr,to,by)
            self.Hanoi(1,fr,by,to)
            self.Hanoi(n-1,by,fr,to)

        return fr,by,to

    def test_hanoi(self):
        assert self.Hanoi(len([2,3,4,5,6]),[2,3,4,5,6],[],[])==([],[],[2,3,4,5,6])

if __name__ == '__main__':
    unittest.main()