code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

Towerofhanoi

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

public class TowerOfHanoi {
  static void func(int n, String a, String b, String c) {
    if (n == 1) {
      System.out.println("Move 1 disk from " + a + " to " + c);
      return;
    }
    func(n - 1, a, c, b);
    System.out.println("Move disk " + n + " from "+ a + " to " + c);
    func(n - 1, b, a, c);
  }

  public static void main(String[] args) {
    func(3, "Tower A", "Tower B", "Tower C");
  }
}

Created 2021-10-26T03:12:44+01:00, updated 2021-10-26T19:15:26+01:00 · History · Edit