Hanoi Problem Solving:
Hanoi (also known as Hanoi) is a puzzle toy derived from an ancient Indian legend. When big Brahma created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of size. The great Brahma commanded the Brahman to rearrange the discs from below to the other pillars in order of size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a time.
public class Hanoitower {
/**
* Move the PLATE
* TopN: Number of plates moved
* FROM: Starting tower seat
* Inter: Middle Tower seat
* To: Target tower block
*/
public static void Dotower (int. Topn,char From,char Inter,char to) {
if (TopN = = 1) {
SYSTEM.OUT.PRINTLN ("Plate 1, from" + from + "tower seat to" + to + "tower seat");
} else {
Dotower (TopN-1, from, to, Inter);
SYSTEM.OUT.PRINTLN ("plate" + TopN + ", from the" + from + "tower seat to the" + to + "tower seat");
Dotower (TopN-1, Inter, from, to);
}
}
}
Test:
public class Testhanoitower {
public static void Main (string[] args) {
Hanoitower.dotower (5, ' A ', ' B ', ' C ');
}
}
This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1696502
Lapping data structures and algorithms-advanced application of 07 recursion