Java data structure and algorithm learning Tower example _java

Source: Internet
Author: User

Copy Code code as follows:

Package com.tiantian.algorithms;
/**
* _|_1 | |
* __|__2 | |
* ___|___3 |            | (1). Move 4 pieces of wood from A to C.
* ____|____4 | |
* A B C
*
*     |                | |
*     | _|_1 |
*     |            __|__2 | To achieve the effect (1), you must move the 1, 2, 3 pieces of wood to B, so that you can move 4 to C
* ____|____4 ___|___3 | such as: "Call (XX)" in code
* A B C
*
*     |                | |
*     | _|_1 |
*     |            __|__2 | At this point, the title becomes the move 3 pieces of wood on B to C, back to the title (1)
*     | ___|___3 ____|____4 such as: "Call (YY)" in code
* A B C
*
* and then loop the process
*
* @author Wangjie
* @version Date Created: 2013-3-4 4:09:53
*/
public class Hanoitowertest {
public static void Main (string[] args) {
Dotowers (4, ' A ', ' B ', ' C ');
}

public static void Dotowers (int topn, char from, Char Inter, char to) {
if (topn = = 1) {
System.out.println ("The last block 1 from the" + from + "move to" + to);
}else{
Dotowers (TopN-1, from, to, Inter); Call (XX)
System.out.println ("Put the block" + TOPN + "from" + from + "move to" + to);
Dotowers (TopN-1, Inter, from, to); Call (YY)
}

}
}

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.