Java Solution Hanoi Tower problem Example _java

Source: Internet
Author: User

Ideas are as follows:

To achieve the 3-order Hanoi solution, that is, when the initial state, a on the top from the bottom there are three plates, respectively, 1th Plate, 2nd plate and 3rd plate, of which 1th plate is the smallest, 3rd plate the largest;
Determine the number of remaining plates, if only one plate to exit the iteration, if there is more than one plate to continue the iteration.
The code is as follows:

Copy Code code as follows:

public class Hanoitower {
public static void Movedish (int level, char from, Char Inter, char to) {
if (level = 1) {//If there is only one plate, exit the iteration
System.out.println ("from" + from + "Move plate 1th" + to);
else {//If there is more than one plate, continue the iteration
Movedish (Level-1, from, to, Inter);
System.out.println ("from" + from + "Move plate" + Level + "number" + to);
Movedish (Level-1, Inter, from, to);
}
}

public static void Main (string[] args) {
int ndisks = 3;//set Hanoi to 3-step
Movedish (ndisks, ' A ', ' B ', ' C '); implementation of mobile algorithm
}
}

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.