File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
Recursive Algorithms/Java Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments