(汉诺塔问题) 给定三根柱子,分别标记为 A、B 和 C。初始状态下,柱子 A 上有若干个圆盘,这些圆盘从上到下按从小到大的顺序排列。任务是将这些圆盘全部移到柱子 C 上,且必须保持原有顺序不变。在移动过程中,需要遵守以下规则:
试补全程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> #include <vector> using namespace std; void move(char src, char tgt) { cout << "从柱子" << src << "挪到柱子" << tgt << endl; } void dfs(int i, char src, char tmp, char tgt) { if (i == ___①___) { move(___②___; return; } dfs(i - 1, ___③___); move(src, tgt); dfs(___⑤___, ___④___); } int main() { int n; cin >> n; dfs(n, 'A', 'B', 'C'); } |
0 of 5 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 5 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
① 处应填( )
② 处应填( )
③ 处应填( )
④ 处应填( )
⑤ 处应填( )