Lapping data structures and algorithms-advanced application of 07 recursion

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.