A hannotta example of Java data structure and algorithm learning

  • 2020-04-01 02:53:11
  • OfStack


package com.tiantian.algorithms;

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(" Finally, put the block 1 from " + from + " Move to the " + to);
        }else{
            doTowers(topN - 1, from, to, inter); //Call (XX)
            System.out.println(" The wood " + topN + " from " + from + " Move to the " + to);
            doTowers(topN - 1, inter, from ,to); //Call (YY)
        }

    }
}


Related articles: