From 59cdc9b3ac4f73826ad3b3550872111f236a94f6 Mon Sep 17 00:00:00 2001 From: Reza Date: Tue, 22 Apr 2025 23:02:12 +0330 Subject: [PATCH] completed ! --- src/components/Applications/Hanoi/agent.js | 71 +++++++++++++++++----- 1 file changed, 57 insertions(+), 14 deletions(-) diff --git a/src/components/Applications/Hanoi/agent.js b/src/components/Applications/Hanoi/agent.js index d957254..fa4786e 100644 --- a/src/components/Applications/Hanoi/agent.js +++ b/src/components/Applications/Hanoi/agent.js @@ -53,26 +53,69 @@ Remember: // } export const hanoi = (from, via, to, n, moves) => { - alert("complete hanoi function first!") + if (n === 1) { + moves.push([from, to]); + return; + } -} + hanoi(from, to, via, n - 1, moves); + moves.push([from, to]); + hanoi(via, from, to, n - 1, moves); +}; export const exhanoi_1 = (A, B, C, n, moves) => { - alert("complete exhanoi_1 function first!") - -} + const transfer = () => { + hanoi(C, A, B, 3 * n - 1, moves); + moves.push([A, C]); + hanoi(B, A, C, 3 * n - 1, moves); + }; + + if (n === 1) { + hanoi(C, A, B, 2, moves); + moves.push([A, C]); + hanoi(B, A, C, 2, moves); + } else { + exhanoi_1(A, B, C, n - 1, moves); + transfer(); + } +}; export const exhanoi_2 = (A, B, C, n, moves) => { - alert("complete exhanoi_2 function first!") + if (n === 1) { + moves.push([A, C], [B, C]); + return; + } -} - -export const exhanoi_3 = (A, B, C, n, moves) => { - alert("complete exhanoi_3 function first!") + exhanoi_2(A, B, C, n - 1, moves); + hanoi(C, A, B, 2 * n - 2, moves); + moves.push([A, C]); + hanoi(B, A, C, 2 * n - 1, moves); +}; -} +export const exhanoi_3 = (A, B, C, n, moves) => { + if (n === 1) { + moves.push([C, B], [A, B]); + hanoi(B, A, C, 3, moves); + return; + } + + exhanoi_3(A, B, C, n - 1, moves); + hanoi(C, B, A, 3 * n - 3, moves); + moves.push([C, B]); + hanoi(A, C, B, 3 * n - 2, moves); + hanoi(B, A, C, 3 * n, moves); +}; export const exhanoi_4 = (A, B, C, n, moves) => { - alert("complete exhanoi_4 function first!") - -} \ No newline at end of file + if (n === 1) { + moves.push([C, B]); + hanoi(B, C, A, 3, moves); + hanoi(A, B, C, 6, moves); + return; + } + + exhanoi_4(A, B, C, n - 1, moves); + hanoi(C, A, B, 6 * n - 5, moves); + hanoi(B, C, A, 6 * n - 3, moves); + hanoi(A, B, C, 6 * n, moves); +};