freeCodeCamp/curriculum/challenges/chinese/08-coding-interview-prep/rosetta-code/towers-of-hanoi.chinese.md

2.9 KiB
Raw Blame History

title id challengeType videoUrl localeTitle
Towers of Hanoi 5951ed8945deab770972ae56 5 河内的塔

Description

任务:

解决河内塔问题。

您的解决方案应该接受光盘数量作为第一个参数,并使用三个字符串来识别三个光盘堆栈中的每一个,例如towerOfHanoi(4, 'A', 'B', 'C') 。该函数应该返回一个包含移动列表的数组数组source - > destination。例如数组[['A', 'C'], ['B', 'A']]表示第一个移动是将光盘从堆栈A移动到C第二个移动是移动一个从堆栈B到A的光盘

Instructions

Tests

tests:
  - text: <code>towerOfHanoi</code>是一个功能。
    testString: 'assert(typeof towerOfHanoi === "function", "<code>towerOfHanoi</code> is a function.");'
  - text: ''
    testString: 'assert(res3.length === 7, "<code>towerOfHanoi(3, ...)</code> should return 7 moves.");'
  - text: '<code>towerOfHanoi(3, &quot;A&quot;, &quot;B&quot;, &quot;C&quot;)</code>应返回[[“A”“B”][“A”“C”][“B”“C”][ “A” “B”][ “C” “A”][ “C” “B”][ “A” “B”]]“)。'
    testString: 'assert.deepEqual(towerOfHanoi(3, "A", "B", "C"), res3Moves, "<code>towerOfHanoi(3, "A", "B", "C")</code> should return [["A","B"],["A","C"],["B","C"],["A","B"],["C","A"],["C","B"],["A","B"]].");'
  - text: '<code>towerOfHanoi(5, &quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;)</code>第10 <code>towerOfHanoi(5, &quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;)</code>应为Y  - &gt; X.'
    testString: 'assert.deepEqual(res5[9], ["Y", "X"], "<code>towerOfHanoi(5, "X", "Y", "Z")</code> 10th move should be Y -> X.");'
  - text: '<code>towerOfHanoi(7, &quot;A&quot;, &quot;B&quot;, &quot;C&quot;)</code>前十个动作是[[“A”“B”][“A”“C”][“B”“C”] [ “A” “B”][ “C” “A”][ “C” “B”][ “A” “B”][ “A” “C”] [ “B” “C”][ “B” “A”]]“)。'
    testString: 'assert.deepEqual(towerOfHanoi(7, "A", "B", "C").slice(0, 10), res7First10Moves, "<code>towerOfHanoi(7, "A", "B", "C")</code> first ten moves are [["A","B"],["A","C"],["B","C"],["A","B"],["C","A"],["C","B"],["A","B"],["A","C"],["B","C"],["B","A"]].");'

Challenge Seed

function towerOfHanoi (n, a, b, c) {
  // Good luck!
  return [[]];
}

After Test

console.info('after the test');

Solution

// solution required