publicstaticvoidmain(String[] args){ // 定义Cobb的梦 // 假的Cobb,他的陀螺不会停 Cobb fakeCobb = new Cobb(false); fakeCobb.prev = new Cobb(false); fakeCobb.prev.prev = new Cobb(false); fakeCobb.prev.prev.prev = new Cobb(false); fakeCobb.prev.prev.prev.prev = new Cobb(false); fakeCobb.prev.prev.prev.prev.prev = new Cobb(false); fakeCobb.prev.prev.prev.prev.prev.prev = new Cobb(false);
// 真实的Cobb,他的陀螺会停 Cobb realCobb = new Cobb(true); fakeCobb.prev.prev.prev.prev.prev.prev.prev = realCobb;
// 递归,求Cobb梦的大小 // fackCobb开始GoBack int DreamSize = GoBack(fakeCobb); System.out.println(DreamSize);
publicclassHaNuoTa{ // 请把1~N层圆盘 从左 -> 右 publicstaticvoidleftToRight(int n){ if (n == 1) { // base case System.out.println("Move 1 from left to right"); return; } leftToMid(n - 1); System.out.println("Move " + n + " from left to right"); midToRight(n - 1); }
// 请把1~N层圆盘 从左 -> 中 publicstaticvoidleftToMid(int n){ if (n == 1) { System.out.println("Move 1 from left to mid"); return; } leftToRight(n - 1); System.out.println("Move " + n + " from left to mid"); rightToMid(n - 1); }
publicstaticvoidrightToMid(int n){ if (n == 1) { System.out.println("Move 1 from right to mid"); return; } rightToLeft(n - 1); System.out.println("Move " + n + " from right to mid"); leftToMid(n - 1); }
publicstaticvoidmidToRight(int n){ if (n == 1) { System.out.println("Move 1 from mid to right"); return; } midToLeft(n - 1); System.out.println("Move " + n + " from mid to right"); leftToRight(n - 1); }
publicstaticvoidmidToLeft(int n){ if (n == 1) { System.out.println("Move 1 from mid to left"); return; } midToRight(n - 1); System.out.println("Move " + n + " from mid to left"); rightToLeft(n - 1); }
publicstaticvoidrightToLeft(int n){ if (n == 1) { System.out.println("Move 1 from right to left"); return; } rightToMid(n - 1); System.out.println("Move " + n + " from right to left"); midToLeft(n - 1); }
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
# 请把1~N层圆盘 从左 -> 右 defleft_to_right(n): if n == 1: print("Move 1 from left to right") return left_to_mid(n - 1) print("Move " + str(n) + " from left to right") mid_to_right(n - 1)
# 请把1~N层圆盘 从左 -> 中 defleft_to_mid(n): if n == 1: print("Move 1 from left to mid") return left_to_right(n - 1) print("Move " + str(n) + " from left to mid") right_to_mid(n - 1)
defright_to_mid(n): if n == 1: print("Move 1 from right to mid") return right_to_left(n - 1) print("Move " + str(n) + " from right to mid") left_to_mid(n - 1)
defmid_to_right(n): if n == 1: print("Move 1 from mid to right") return mid_to_left(n - 1) print("Move " + str(n) + " from mid to right") left_to_right(n - 1)
defmid_to_left(n): if n == 1: print("Move 1 from mid to left") return mid_to_right(n - 1) print("Move " + str(n) + " from mid to left") right_to_left(n - 1)
defright_to_left(n): if n == 1: print("Move 1 from right to left") return right_to_mid(n - 1) print("Move " + str(n) + " from right to left") mid_to_left(n - 1)
if __name__ == '__main__': left_to_right(3)
运行结果:
1 2 3 4 5 6 7
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
publicstaticvoidfunc(int N, String from, String to, String other){ if (N == 1) { // base System.out.println("Move 1 from " + from + " to " + to); } else { func(N - 1, from, other, to); System.out.println("Move " + N + " from " + from + " to " + to); func(N - 1, other, to, from); } }
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
示例代码:
1 2 3 4 5 6 7 8 9 10 11
deffunc(n, froms, tos, other): if n == 1: print("Move 1 from " + froms + " to " + tos) else: func(n - 1, froms, other, tos) print("Move " + str(n) + " from " + froms + " to " + tos) func(n - 1, other, tos, froms)
if __name__ == '__main__': func(3, "left", "right", "mid")
运行结果:
1 2 3 4 5 6 7
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
publicstaticvoidmain(String[] args){ Node head = new Node(0); head.next = new Node(1); head.next.next = new Node(2); head.next.next.next = new Node(3); head.next.next.next.next = new Node(4); head.next.next.next.next.next = new Node(5); head.next.next.next.next.next.next = new Node(6); head.next.next.next.next.next.next.next = new Node(7); head.next.next.next.next.next.next.next.next = new Node(8); head.next.next.next.next.next.next.next.next.next = new Node(9);
publicstaticvoidmain(String[] args){ Node head = new Node(0); head.next = new Node(1); head.next.next = new Node(2); head.next.next.next = new Node(3); head.next.next.next.next = new Node(4); head.next.next.next.next.next = new Node(5); head.next.next.next.next.next.next = new Node(6); head.next.next.next.next.next.next.next = new Node(7); head.next.next.next.next.next.next.next.next = new Node(8); head.next.next.next.next.next.next.next.next.next = new Node(9);
defprocess(q): rnt = [] if q.empty(): rnt.append("") return rnt t = q.get() s_yes = t s_no = "" for s in process(q): rnt.append(s_yes + s) rnt.append(s_no + s) return rnt
if __name__ == '__main__': s = ['a', 'b', 'c'] q = Queue() for c in s: q.put(c) ans = process(q) print(ans)
/** * @param str 固定参数 * @param index 来到了str[index]字符,index是位置 * @param ans * @param path 之前已经决定的不能改变,即使path * @return */ publicstaticvoidprocess(char[] str, int index, ArrayList<String> ans, String path){ if (index == str.length) { ans.add(path); return; } // 要了index位置的字符 process(str, index + 1, ans, path + String.valueOf(str[index])); // 没有要index位置的字符 process(str, index + 1, ans, path); }
publicstaticvoidmain(String[] args){ char[] str = "abc".toCharArray(); String path = ""; ArrayList<String> ans = new ArrayList<>(); process(str, 0, ans, path); System.out.println(ans); } }
运行结果:
1
[abc, ab, ac, a, bc, b, c, ]
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
defprocess(s, index, ans, path): if index == len(s): ans.append(path) return # 要了index位置的字符 process(s, index + 1, ans, path + s[index]) # 没有要index位置的字符 process(s, index + 1, ans, path)
if __name__ == '__main__': s = ['a', 'b', 'c'] path = '' ans = [] process(s, 0, ans, path) print(ans)
publicclassPermutation{ /** * @param str 固定参数 * @param index 来到了str[index]字符,index是位置 * @param ans * @param path 之前已经决定的不能改变,即使path * @return */ publicstaticvoidprocess(ArrayList<Character> str, ArrayList<String> ans, String path){ if (str.isEmpty()) { ans.add(path); return; } for (int i = 0; i < str.size(); i++) { ArrayList<Character> temp = (ArrayList<Character>) str.clone(); temp.remove(i); process(temp, ans, path + String.valueOf(str.get(i))); } }
publicstaticvoidmain(String[] args){ ArrayList<Character> l = new ArrayList<>(); l.add('甲'); l.add('乙'); l.add('丙'); ArrayList<String> ans = new ArrayList<>(); String path = ""; process(l, ans, path); System.out.println(ans); } }
运行结果:
1
[甲乙丙, 甲丙乙, 乙甲丙, 乙丙甲, 丙甲乙, 丙乙甲]
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
defprocess(s, ans, path): if len(s) == 0: ans.append(path) return for i in range(len(s)): temp = s.copy() temp.pop(i) process(temp, ans, path + s[i])
if __name__ == '__main__': l = ['甲', '乙', '丙'] ans = [] path = "" process(l, ans, path) print(ans)