8000 Merge pull request #1100 from HardikSoni11/patch-2 · glitches-coder/Algorithms@fedcc2d · GitHub
[go: up one dir, main page]

Skip to content

Commit fedcc2d

Browse files
authored
Merge pull request VAR-solutions#1100 from HardikSoni11/patch-2
Create TOH.java
2 parents 04d81c2 + eb2bb87 commit fedcc2d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Recursive Algorithms/Java/TOH.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
5+
class TOH {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int n = Integer.parseInt(br.readLine());
9+
towerOfHanoi(n, 'A', 'C', 'B');
10+
}
11+
12+
static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
13+
{
14+
if (n == 1)
15+
{
16+
System.out.println("Move disk 1 from rod " + from_rod + " to rod " + to_rod);
17+
return;
18+
}
19+
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
20+
System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + to_rod);
21+
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
22+
}
23+
}

0 commit comments

Comments
 (0)
0