code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

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