Tower of Hanoi (also known as Hanoi) is an ancient and legendary educational toy in India. When I created the world, da fan Tian made three diamond pillars, stacked 64 gold disks from bottom to top in order of size. The great fan Tian command Brahman re-placed the disc from below in order of size on another pillar. It is also stipulated that the disc cannot be enlarged on a small disc, and only one disc can be moved between the three pillars at a time.
Public class Hanoi {/***** @ param n * Number of plates * @ param A * Source seat * @ param B * auxiliary seat * @ param C * destination seat */public void hanoi (int n, char A, char B, char C) {if (n = 1) {move (A, C);} else {hanoi (n-1, A, C, b); move (A, C); hanoi (n-1, B, A, C);} // print the moving path private void move (char A, char C) {System. out. println ("Direction:" + A + "--->" + C);} public static void main (String [] args) {Hanoi hanoi = new Hanoi (); hanoi. hanoi (3, '1', '2', '3 ');}}
Print result:
Direction:1--->3 Direction:1--->2 Direction:3--->2 Direction:1--->3 Direction:2--->1 Direction:2--->3 Direction:1--->3