2014年2月10日星期一

For A1 feeling

For whole week, I was struggling for my Assignment 1. Now I have almost done this relatively huge assignment. As viewing whole process of implementing online schedule, it is the practice for using class from step 1 to step 4 and the recursion, the extreme key part of whole program, is in step 5 and step 6. At that time for fixing the algorithm of Hanoi Tower, I did the research for resolving this historical problem. Although the method for moving is quite easy, it is unknown question whether it is the most efficient way to solve problem if changing with different numbers of stools. As you known, n-1 cheeses should be moved to the spare stool if it is the fewest steps of three stools. Whereas, the fewest steps for four stools is not made n-1 cheeses to spare stools. After my testing continuously, k, n-k cheeses, should be 3 and I found the shortest steps for 8 cheeses in four stools is 33. However,I have a very weird problem here. It would give me an error on 12 cheeses, in total, doing automatically. The most strange thing is that everything is fine except this number. The algorithm that I have written is on the following part.
def move_cheeses(self, n: int, source: int, intermediate1: int,
                     intermediate2: int, destination: int) -> None:
        if n > 2:
            self.move_cheeses(n - 3, source, destination, intermediate2,
                              intermediate1)
            self.move_cheeses(2, source, intermediate1, destination,
                              intermediate2)
            self.move_cheeses(1, source, intermediate1, intermediate2,
                              destination)
            self.move_cheeses(2, intermediate2, intermediate1, source,
                              destination)
            self.move_cheeses(n - 3, intermediate1, source, intermediate2,
                              destination)
        elif n > 1:
            self.move_cheeses(1, source, intermediate1, destination,
                              intermediate2)
            self.move_cheeses(1, source, intermediate1, intermediate2,
                              destination)
            self.move_cheeses(1, intermediate2, intermediate1, source,
                              destination)
        else:  # just one cheese --- no recursion required!
            print ('Move disk from', source, 'to', destination)

没有评论:

发表评论