From fcbde897336d306131d712a0674151784c3dfba0 Mon Sep 17 00:00:00 2001 From: hk513 <116027288+hk513@users.noreply.github.com> Date: Tue, 18 Oct 2022 22:09:58 +0530 Subject: [PATCH] tower of hanoi Tower Of Hanoi 1. There are 3 towers. Tower 1 has n disks, where n is a positive number. Tower 2 and 3 are empty. 2. The disks are increasingly placed in terms of size such that the smallest disk is on top and largest disk is at bottom. 3. You are required to 3.1. Print the instructions to move the disks. 3.2. from tower 1 to tower 2 using tower 3 3.3. following the rules 3.3.1 move 1 disk at a time. 3.3.2 never place a smaller disk under a larger disk. 3.3.3 you can only move a disk at the top. Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is.Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you. --- towerofhanoi.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 towerofhanoi.java diff --git a/towerofhanoi.java b/towerofhanoi.java new file mode 100644 index 0000000..0f99354 --- /dev/null +++ b/towerofhanoi.java @@ -0,0 +1,28 @@ +import java.io.*; +import java.util.*; + +public class Main { + + public static void main(String[] args) throws Exception { + Scanner scn = new Scanner(System.in); + int n = scn.nextInt(); + int src = scn.nextInt(); + int dest = scn.nextInt(); + int help = scn.nextInt(); + toh(n, src, dest, help); + + } + + public static void toh(int n, int src, int dest, int help){ + if(n==0){ + return; + } + toh(n-1,src,help,dest); + System.out.println(n + "[" + src + " -> "+dest + "]"); + toh(n-1,help,dest,src); + return; + + + } + +}