Towers of Honoi
Write a code for Tower of Honoi problem. There are n disks, three rods (left, center, and right). Initially all the n disks are at left side rod. We need to move these n disks from left to right side rod with help of center rod (auxiliary rod) one disk at a time. Also, disk 1 is smaller, disk n is larger and smaller disk should be at the top of larger disk while moving disks.
Sol:
1
2
3
4
5
6
7
8
9
| void Honoi(int n, String left, String right, String centre) { if(n>0) { Honoi(n-1, left, centre, right); System.out.println(" Move disk " + n + " from " + left + " to " + right); Honoi(n-1, centre, right, left); } } |
No comments:
Post a Comment