This simple recursive solution works for any number of disks. Try giving a different number of dicks as user input and check the output. Hanoi(disks - 1, auxiliary, source, target)ĭisks = int(input('Enter number of disks: '))Ĭase 1: when number of disk is 4 Enter number of disks: 4Ĭase 2: when number of disk is 2 Enter number of disks: 2Ĭase 3: when number of disk is 1 Enter number of disks: 1 Your task is to reorganize the disks according to the example, in the least amount of moves possible. You will have three towers with different colored disks stacked on them, and an example of how the towers should look like. Then enter the following Most Popular Secret Codes, Those used commonly. Move (N-1) disks from peg B to peg C using the intermediate peg A.ĭef hanoi(disks, source, auxiliary, target): The Pymetrics Tower Game is a form of the well-known Hanoi Towers game. The current position of ONE HANOI is at North Sea (coordinates 51.Move disk N (largest) from peg A to peg C using intermediate peg B.Move the first(N-1) disks from peg A to peg B using intermediate peg C.When N>1, then you can divide the original problem into three subproblems and solve them sequentially as follows. The problem has a recursive nature which leads to a straight forward recursive solution.Īssume there are N disks, if N=1, then you simply shift the disk from peg A to peg C. In case of three disks you can find the solution manually but for a larger number of disks like four or more than four then the situation becomes quite complex. Here is how you can solve the Tower of Hanoi problem for three disk.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |