[알고리즘] 하노이의 탑 Javascript로 풀어보기

하노이의 탑

풀어보았다.

소스코드

    function Hanoi(plate, departure, dropBy, destination) {
    if (plate === 1) {
        document.getElementById('responseDiv').innerHTML = `${plate}을 ${departure}에서 ${destination}로 이동`
    } else if (plate > 1) {
        Hanoi(plate - 1, 'A', 'B', 'C');
        document.getElementById('responseDiv').innerHTML = `${plate - 1}을 ${departure}에서 ${destination}로 이동`
        Hanoi(plate - 1, 'C', 'A', 'B');
    }
}

Hanoi(3, 'A', 'B', 'C');
<!DOCTYPE html>

<html lang="ko">
  <head>
    <meta charset="utf-8" />
  </head>

  <body>
    <div id="responseDiv">11</div>
    <script type="text/javascript" src="index.js"></script>
  </body>
</html>

이 글이 도움이 되었나요?

신고하기
0분 전
작성된 댓글이 없습니다. 첫 댓글을 달아보세요!
    댓글을 작성하려면 로그인이 필요합니다.