Lab3003
  • FacultyAlgorithmChallenge
  • README
  • Question Bank
  • Python Documentation
    • BetterMap
    • Board
    • Card
    • Circle
    • Deck
    • README.md
    • Guide For Bookmarklet
    • Hand
    • HashMap
    • Hist
    • Kangaroo
    • LinearMap
    • Markov
    • Point
    • PokerDeck
    • PokerHand
    • Rectangle
    • State
    • Test
    • ThinkPython
    • ThinkPython2
    • Time
    • Think Python
    • thinkpython
  • Files
  • Image Files
  • lab
    • InsertSortTDD
    • countingBits
    • cs3003mergeSortLectureGuideLine
    • index_min
    • insort
    • One-line factorial function in Python
  • Manual for Python Lab
    • COURSE OBJECTIVES
    • 1_timelines
    • FDPNotes
    • PC-2_Python
    • lab1-kgashok.py
    • Lab 10 :Matrix Multiplication
    • Lab 11: Programs that take command line arguments (word count)
    • Lab 11: Programs that take command line arguments
    • Lab 12: Compute the most frequently used words from the text file
    • Lab 12: Find the most frequent words in a text read from a file
    • Lab 12a: File content sorter
    • Lab 2: Find the square root of a number (Newton’s method)
    • Lab 3: Compute power of a number (Exponentiation)
    • Lab 3: Exponentiation (power of a number)
    • lab4-binaraysearch-anvar.py
    • Solution for Binary Search
    • lab4-joelanandraj-binarysearch.py
    • Lab 4: Linear and Binary Search
    • lab4-lin-anvar.py
    • Linear Searcher
    • Lab 5 : find n prime numbers
    • lab6-kgashok.py
    • lab7-kgashok.py
    • lab8-kgashok.py
    • Lab 8: Selection and Insertion sort using Python
    • Merge Sort
    • Quick Sort
    • labSheet
    • pc0
    • sortPythonic
    • Sorting
  • misc
    • Bookmarklets
    • Guide For Bookmarklet
    • pythonResources
    • FDP for Python
    • Agenda for Workshop
      • Agenda
  • notes
    • Problem Set
    • InsertSortTDD
    • MergeSort
    • cs3003-unit1-notes
    • cs3003-unit3
    • cs3003-unit4-examples
    • Unit 4 Lists, Tuples and Dictionaries
    • cs3003insertsortLectureGuide
    • cs3003mergeSortLectureGuideLine
    • Designing and Delivering Effective Lectures
    • histogram.py
    • selectSortLectureGuide
  • Sandbox to try ThinkPy2 code
    • Code from ThinkPython Github repository
  • Important Topics
    • 3003-syllabus
    • GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY L T P C
    • Unit II Version 2
    • Unit IV material
    • UNIT III CONTROL FLOW, FUNCTIONS
    • Unit 1 Algorithm Problem Solving
    • Unit_V_Notes
    • UpdatedSyllabus
    • glossary
    • glossaryGeneration.py
    • importantTopics-kgashok
    • memCSV
    • Tower of Hanoi
    • Notes for Unit 2 - Illustrative programs
    • unit5-updated_notes
    • unit_1_notes
    • Unit 3 Control Flow Version 1
    • unit 3
      • UNIT III CONTROL FLOW, FUNCTIONS
    • unit_i_img
Powered by GitBook
On this page
  • CloudCoder link
  • Diagrammatic representation
  • Visualize it!
  • Play with the code!
  • Virtually play Tower of Hanoi
  1. Important Topics

Tower of Hanoi

CloudCoder link

http://j.mp/towerOfHanoiCC

The puzzle was invented by the French mathematician Édouard Lucas in 1883. There is a story about an Indian temple in Kashi Vishwanath which contains a large room with three time-worn posts in it, surrounded by 64 golden disks. Brahmin priests, acting out the command of an ancient prophecy, have been moving these disks in accordance with the immutable rules of Brahma since that time. The puzzle is therefore also known as the Tower of Brahma puzzle. According to the legend, when the last move of the puzzle is completed, the world will end.

#include <stdio.h>

void moveDisk(char* from, char* to) {
  printf(" Move top disk from tower %s to tower %s\n", from, to);
}

void TowersOfHanoi(int n, char* from, char* to, char* temp) {
  // Move top n disks from tower "from" to tower "to",
  // use tower "temp" for intermediate storage.
  if (n > 0) {
    TowersOfHanoi(n - 1, from, temp, to);  // recursion
    moveDisk(from, to);
    // Move n-1 disks from temp to the destination
    TowersOfHanoi(n - 1, temp, to, from);  // recursion
  }
}

int main() {
  TowersOfHanoi(3, "Tower1", "Tower2", "Inter");
  return 0;
}

OUTPUT

TowerOfHanoi(1, "Tower 1", "Tower 2", "Inter")

Move top disk from tower Tower1 to tower Tower2

TowerOfHanoi(2, "Tower 1", "Tower 2", "Inter")

Move top disk from tower Tower1 to tower Inter
Move top disk from tower Tower1 to tower Tower2
Move top disk from tower Inter to tower Tower2

TowerOfHanoi(3, "Tower 1", "Tower 2", "Inter")

Move top disk from tower Tower1 to tower Tower2
Move top disk from tower Tower1 to tower Inter
Move top disk from tower Tower2 to tower Inter
Move top disk from tower Tower1 to tower Tower2
Move top disk from tower Inter to tower Tower1
Move top disk from tower Inter to tower Tower2
Move top disk from tower Tower1 to tower Tower2

Diagrammatic representation

  • https://en.wikibooks.org/wiki/Data_Structures/Stacks_and_Queues#Towers_of_Hanoi

  • http://interactivepython.org/runestone/static/pythonds/Recursion/TowerofHanoi.html

Visualize it!

http://bit.ly/hanoiVisual

Play with the code!

Virtually play Tower of Hanoi

  • http://bit.ly/hanoiPlay

  • http://bit.ly/hanoiPlay2

PreviousmemCSVNextNotes for Unit 2 - Illustrative programs

Last updated 3 years ago

C++ version
Python version