Data structure & Algorithm
  • Initial page
  • 1212
  • 121231
  • 2.algorithm
    • 2.1.backtracking
      • 2.1.1.連續序列的排列組合(可能會重複)
      • 2.1.3.一串數列中任取n個數字, 總共有幾種組合
      • 2.1.2.一串數列中取n個數字, 共有幾種組合
    • 2.5.Quick sort
    • 2.6.2.6.廣度優先搜尋 (Breadth-first Search)
    • 2.2.Binary Search
    • 2.1.Backtracking
    • 2.4.河內塔 (Tower of Hanoi)
    • 2.7.動態規劃
    • 2.8.深度優先搜尋 (Depth-first Search)
    • 2.7.二分搜尋法
    • 2.3.分治法 (Divide and Conquer)
  • 2.Count and Say
  • 1.Leetcode Algorithm Practice
  • 2-count-and-say
    • c-solution
    • javascript-solution
  • Algorithm
  • 123
Powered by GitBook
On this page

Was this helpful?

  1. 2.algorithm

2.4.河內塔 (Tower of Hanoi)

  • Introduction

    • 規則如下

      • 1.有三根竿子,例如編號為A、B和C,竿子上面可串中空圓盤。

      • 2.於A竿子放入N個盤子開始,盤子由下至上變小。

      • 3.一次只能移動一個盤子。

      • 4.大盤子不能再小盤子上面。

      • 5.目標將全部盤子移動到C竿子。

    • 解法

  • 程式碼

      void move(int disks, int from, int to)
      {
          if(disks == 1)
          {
              cout << "Move from " << from << " to " << to << endl;
              return;
          }
          int relay = 6 - from - to;
          move(disks - 1, from, relay);
          move(1, from, to);
          move(disks - 1, relay, to);
      }
Previous2.1.BacktrackingNext2.7.動態規劃

Last updated 5 years ago

Was this helpful?