# 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.

```c
#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!

* [C++ version](https://repl.it/@kgashok/TowerOfHanoi)
* [Python version](https://repl.it/@kgashok/demoOfHanoi)

### Virtually play Tower of Hanoi

* <http://bit.ly/hanoiPlay>
* <http://bit.ly/hanoiPlay2>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kgisl.gitbook.io/lab3003/units/towerofhanoi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
