algorithmic puzzles - #83 restricted tower of hanoi

29
#83 Restricted Tower of Hanoi NickWarm a.k.a WG

Upload: -

Post on 13-Apr-2017

55 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Algorithmic puzzles - #83  restricted tower of hanoi

#83 Restricted Tower of Hanoi

NickWarm

a.k.a

WG

Page 2: Algorithmic puzzles - #83  restricted tower of hanoi

原始的河內塔

A CB

⺫⽬目標:把碟⼦子移動到C

Page 3: Algorithmic puzzles - #83  restricted tower of hanoi

簡化問題

A CB6

321

45

⽤用數字來代表盤⼦子⼤大⼩小 n = 6

Page 4: Algorithmic puzzles - #83  restricted tower of hanoi

條件

• ⼀一次移動⼀一個碟⼦子

• ⼤大的碟⼦子不能疊在⼩小的上⾯面

Page 5: Algorithmic puzzles - #83  restricted tower of hanoi

期待的結果

A CB

321

456

n = 6

Page 6: Algorithmic puzzles - #83  restricted tower of hanoi

⺫⽬目標:最⼤大的碟⼦子,要先放在柱⼦子C

A CB6

321

45

⺫⽬目標

Page 7: Algorithmic puzzles - #83  restricted tower of hanoi

所以:上⾯面的碟⼦子,要先放在柱⼦子B

A CB6

321

45

先把n-1個碟⼦子從A放到B n = 6

Page 8: Algorithmic puzzles - #83  restricted tower of hanoi

然後:最⼤大的碟⼦子,就能放在柱⼦子C

A CB6

321

45

把1個碟⼦子從A放到C n = 6

Page 9: Algorithmic puzzles - #83  restricted tower of hanoi

最後:把剩下的碟⼦子放到柱⼦子C

A CB

321

45 6

先把n-1個碟⼦子從B放到C n = 6

Page 10: Algorithmic puzzles - #83  restricted tower of hanoi

完成

A CB

321

45

6

n = 6

Page 11: Algorithmic puzzles - #83  restricted tower of hanoi

整個流程

n-1個碟⼦子從A移到B

1個碟⼦子從A移到C

n-1個碟⼦子從B移到C

河內塔的演算法 T(n) = 2T(n-1)+1

n-1個 碟⼦子

移動兩次 1個碟⼦子 移動⼀一次

就會變得 ⾮非常直覺

Page 13: Algorithmic puzzles - #83  restricted tower of hanoi

#83 Restricted Tower of Hanoi

Page 14: Algorithmic puzzles - #83  restricted tower of hanoi

條件

• ⼀一次移動⼀一個碟⼦子

• ⼤大的碟⼦子不能疊在⼩小的上⾯面

• ⼀一定要通過中間的柱⼦子B

Page 15: Algorithmic puzzles - #83  restricted tower of hanoi

⺫⽬目標:最⼤大的碟⼦子,要先放在柱⼦子C

A CB6

321

45

⺫⽬目標

依照相同的概念

Page 16: Algorithmic puzzles - #83  restricted tower of hanoi

BUT !!!

Page 17: Algorithmic puzzles - #83  restricted tower of hanoi

條件

• ⼀一次移動⼀一個碟⼦子

• ⼤大的碟⼦子不能疊在⼩小的上⾯面

• ⼀一定要通過中間的柱⼦子B

Page 18: Algorithmic puzzles - #83  restricted tower of hanoi

so,

最⼤大的碟⼦子要先到達B柱⼦子

然後才能到達C柱⼦子

Page 19: Algorithmic puzzles - #83  restricted tower of hanoi

⺫⽬目標:把最⼤大的碟⼦子移到C柱

A CB6

321

45

依照相同的概念

Page 20: Algorithmic puzzles - #83  restricted tower of hanoi

階段性⺫⽬目標:最⼤大的碟⼦子,要先放在柱⼦子B

A CB6

321

45

階段性 ⺫⽬目標

Page 21: Algorithmic puzzles - #83  restricted tower of hanoi

所以:上⾯面的碟⼦子,要先放在柱⼦子C

A CB6

321

45

先把n-1個碟⼦子從A放到C n = 6

Page 22: Algorithmic puzzles - #83  restricted tower of hanoi

接著:把最⼤大的碟⼦子放在柱⼦子B

A CB

321

45

先把1個碟⼦子從A放到B n = 6

6

Page 23: Algorithmic puzzles - #83  restricted tower of hanoi

接著: 把上⾯面的碟⼦子,從柱⼦子C放到柱⼦子B

A CB

321

45

把n-1個碟⼦子從C放到A n = 6

6

Page 24: Algorithmic puzzles - #83  restricted tower of hanoi

接著: 把最⼤大的碟⼦子,從柱⼦子B放到柱⼦子C

A CB

321

45

把1個碟⼦子從B放到C n = 6

6

Page 25: Algorithmic puzzles - #83  restricted tower of hanoi

接著: 把上⾯面的碟⼦子,從柱⼦子A放到柱⼦子C

A CB

321

45

把n-1個碟⼦子從A放到C n = 6

6

Page 26: Algorithmic puzzles - #83  restricted tower of hanoi

完成

A CB

321

45

6

n = 6

Page 27: Algorithmic puzzles - #83  restricted tower of hanoi

整個流程

限制級(?) 河內塔的演算法 T(n) = 3T(n-1)+2

n-1個 碟⼦子

移動三次 1個碟⼦子 移動兩次

就會變得 ⾮非常直覺

Page 29: Algorithmic puzzles - #83  restricted tower of hanoi

參考資料

• 發現演算法

• Towers of Hanoi - Rosetta Code

• Infinite Loop: 【演算】河內塔 - Tower of Hanoi