Chapter 10 Recursion Instructor: Kun-Mao Chao (台大資工 趙坤茂) Rekursiv funksiya


A Classical Case: Towers of Hanoi



Download 1,08 Mb.
bet8/8
Sana01.01.2022
Hajmi1,08 Mb.
#285851
1   2   3   4   5   6   7   8
Bog'liq
2 5400013869605195795

A Classical Case: Towers of Hanoi

  • Copyright ©2004 Pearson Addison-Wesley. All rights reserved.
  • 10-
  • The towers of Hanoi problem involves moving a number of disks (in different sizes) from one tower (or called “peg”) to another.
    • The constraint is that the larger disk can never be placed on top of a smaller disk.
    • Only one disk can be moved at each time
    • Assume there are three towers available.
  • Source
  • Temp
  • Destination

A Classical Case: Towers of Hanoi

  • Copyright ©2004 Pearson Addison-Wesley. All rights reserved.
  • 10-

A Classical Case: Towers of Hanoi

  • Copyright ©2004 Pearson Addison-Wesley. All rights reserved.
  • 10-
  • This problem can be solved easily by recursion.
  • Algorithm:
  • if n is 1 then move disk 1 from the source tower to the destination tower
  • else 1. move n-1 disks from the source tower to the temp tower.
  • 2. move disk n from the source tower to the destination tower. 3. move n-1 disks from the temp tower to the source tower.

A Classical Case: Towers of Hanoi

  • Copyright ©2004 Pearson Addison-Wesley. All rights reserved.
  • 10-
  • The recursive step

A Classical Case: Towers of Hanoi

  • Copyright ©2004 Pearson Addison-Wesley. All rights reserved.
  • 10-
  • The execution result of calling Tower(‘A’, ‘B’, ‘C’,3);

Download 1,08 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish