-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCode_02_Hanoi.java
More file actions
79 lines (64 loc) · 1.92 KB
/
Code_02_Hanoi.java
File metadata and controls
79 lines (64 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package algorithm.basic08;
/**
* @author mood321
* @date 2019/11/12 1:23
* @email [email protected]
*
* move 1 from left to right
* move 2 from left to mid
* move 1 from right to mid
* move 3 from left to right
* move 1 from mid to left
* move 2 from mid to right
* move 1 from left to right
*/
public class Code_02_Hanoi {
public static void hanoi(int n) {
if (n > 0) {
func(n, n, "left", "mid", "right");
}
}
public static void func(int rest, int down, String from, String help, String to) {
if (rest == 1) {
System.out.println("move " + down + " from " + from + " to " + to);
} else {
func(rest - 1, down - 1, from, to, help);
func(1, down, from, help, to);
func(rest - 1, down - 1, help, from, to);
}
}
public static void moveLeftToRight(int N) {
if (N == 1) {
System.out.println("move 1 from left to right");
}
moveLeftToMid(N - 1);
System.out.println("move " + N + "from left to right");
moveMidToRight(N - 1);
}
public static void moveRightToLeft(int N) {
}
public static void moveLeftToMid(int N) {
if (N == 1) {
System.out.println("move 1 from left to mid");
}
moveLeftToRight(N - 1);
System.out.println("move " + N + "from left to mid");
moveRightToMid(N - 1);
}
public static void moveMidToLeft(int N) {
}
public static void moveRightToMid(int N) {
}
public static void moveMidToRight(int N) {
if (N == 1) {
System.out.println("move 1 from mid to right");
}
moveMidToLeft(N - 1);
System.out.println("move " + N + "from mid to right");
moveLeftToRight(N - 1);
}
public static void main(String[] args) {
int n = 3;
hanoi(n);
}
}