HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
👻
개발 기록
/
코딩테스트 스터디
코딩테스트 스터디
/
하노이의 탑(추억의 알고리즘...★)

하노이의 탑(추억의 알고리즘...★)

Link
https://programmers.co.kr/learn/courses/30/lessons/12946
Deadline
Feb 26, 2022
Status
Archived
Type
recursive function
⭐
간만에 재귀 연습차 하노이의 탑을...★ 알고리즘 막 풀던, 옛날이 생각나는군요! -재영
notion image

풀이

은찬
function solution(n) { const answer = []; const dfs = (n, from, tmp, to) => { if(n === 1){ answer.push([from, to]); } else{ dfs(n - 1, from, to, tmp); answer.push([from, to]); dfs(n - 1, tmp, from, to); } } dfs(n, 1, 2, 3); return answer; }
재영
const hanoi = (n, from, to, dropBy, res = []) => { if (n === 1) { res.push([from, to]) return res; } res = hanoi(n - 1, from, dropBy, to, res); res.push([from, to]) res = hanoi(n - 1, dropBy, to, from, res); return res; } function solution(n) { const answer = hanoi(n, 1, 3, 2, []); return answer; }