2021-06-15 00:49:18 -07:00
---
id: 5951ed8945deab770972ae56
2022-02-19 12:56:08 +05:30
title: Torri di Hanoi
2021-06-15 00:49:18 -07:00
challengeType: 5
forumTopicId: 302341
dashedName: towers-of-hanoi
---
# --description--
2022-02-19 12:56:08 +05:30
Risolvi il problema delle [Torri di Hanoi ](https://en.wikipedia.org/wiki/Towers_of_Hanoi "wp: Towers_of_Hanoi" ).
2021-06-15 00:49:18 -07:00
2022-02-19 12:56:08 +05:30
La soluzione dovrebbe accettare il numero di dischi come primo parametro, e tre stringhe usate per identificare ciascuna delle tre pile di dischi, ad esempio `towerOfHanoi(4, 'A', 'B', 'C')` . La funzione dovrebbe restituire un array di array contenente l'elenco delle mosse, sorgente -> destinazione.
2021-06-15 00:49:18 -07:00
2022-02-19 12:56:08 +05:30
Per esempio, l'array `[['A', 'C'], ['B', 'A']]` indica che la prima mossa è stata di spostare un disco dalla pila A alla C, e la seconda mossa è stata quella di spostare un disco dalla pila B alla A.
2021-06-15 00:49:18 -07:00
< p > < / p >
# --hints--
2022-02-19 12:56:08 +05:30
`towerOfHanoi` dovrebbe essere una funzione.
2021-06-15 00:49:18 -07:00
```js
assert(typeof towerOfHanoi === 'function');
```
2022-02-19 12:56:08 +05:30
`towerOfHanoi(3, ...)` dovrebbe restituire 7 mosse.
2021-06-15 00:49:18 -07:00
```js
assert(res3.length === 7);
```
2022-02-19 12:56:08 +05:30
`towerOfHanoi(3, 'A', 'B', 'C')` dovrebbe restituire `[['A','B'], ['A','C'], ['B','C'], ['A','B'], ['C','A'], ['C','B'], ['A','B']]` .
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(towerOfHanoi(3, 'A', 'B', 'C'), res3Moves);
```
2022-02-19 12:56:08 +05:30
La decima mossa di `towerOfHanoi(5, "X", "Y", "Z")` dovrebbe essere Y -> X.
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(res5[9], ['Y', 'X']);
```
2022-02-19 12:56:08 +05:30
Le prime dieci mosse di `towerOfHanoi(7, 'A', 'B', 'C')` dovrebbero essere `['A','B'], ['A','C'], ['B','C'], ['A','B'], ['C','A'], ['C','B'], ['A','B'], ['A','C'], ['B','A']]`
2021-06-15 00:49:18 -07:00
```js
assert.deepEqual(towerOfHanoi(7, 'A', 'B', 'C').slice(0, 10), res7First10Moves);
```
# --seed--
## --after-user-code--
```js
const res3 = towerOfHanoi(3, 'A', 'B', 'C');
const res3Moves = [['A', 'B'], ['A', 'C'], ['B', 'C'], ['A', 'B'], ['C', 'A'], ['C', 'B'], ['A', 'B']];
const res5 = towerOfHanoi(5, 'X', 'Y', 'Z');
const res7First10Moves = [['A', 'B'], ['A', 'C'], ['B', 'C'], ['A', 'B'], ['C', 'A'], ['C', 'B'], ['A', 'B'], ['A', 'C'], ['B', 'C'], ['B', 'A']];
```
## --seed-contents--
```js
function towerOfHanoi(n, a, b, c) {
return [[]];
}
```
# --solutions--
```js
function towerOfHanoi(n, a, b, c) {
const res = [];
towerOfHanoiHelper(n, a, c, b, res);
return res;
}
function towerOfHanoiHelper(n, a, b, c, res) {
if (n > 0) {
towerOfHanoiHelper(n - 1, a, c, b, res);
res.push([a, c]);
towerOfHanoiHelper(n - 1, b, a, c, res);
}
}
```