Data Structure---Hanoi

Source: Internet
Author: User
/*汉诺塔:A座有n个盘子,下面直径比上面大,要借助C座,将n个盘子移到B上,移动过程中大盘子一定在小盘子下面*/package pack;public class Main {    static int n = 10;    public static void main(String[] args) {        han(n,‘A‘,‘B‘,‘C‘);     //将n个盘子借助C,从A到B    }    public static void han(int n,char a,char b,char c) {        if(n==1) {               move(a,b);//只剩一个,直接从A到B         }        else {            han(n-1,a,c,b);   //将n-1个盘子借助B,从A到C               move(a,b);            han(n-1,c,b,a);   //将n-1个盘子借助A,从C到B        }       }    public static void move(char a,char b) {        System.out.println(a+"--->"+b);    }}

Data Structure---Hanoi

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.