UMBC CMSC 201 Fall '06 CSEE | 201 | 201 F'06 | lectures | news | help |

- The Tower of Hanoi is a puzzle consisting of three pegs, and a number of discs of different sizes which can slot onto any peg.
- The puzzle starts with the discs stacked in order of size on one peg, smallest at the top.
- The object of the game is to move the entire stack to another peg, obeying
the following rules:
- only one disc may be moved at a time
- a disc can only be placed onto a larger disc

There is a backstory involving Zen monks and 64 golden disks, but it's invented.

Here's a simple recursive algorithm for the TOH

Preliminaries

- Label the pegs A, B and C
- Assume there are N discs properly stacked on peg A

To move n discs from peg A to peg B:

- move n-1 discs from A to C.
- move the remaining disc from A to B.
- move n-1 discs from C to B.

/*************************************************** * File: toh.c * Author: Tim Finin * Date: 4/12/2005 * Modified by: Sue Evans * Date: 11/5/05 * * A recursive solution for the Towers Of Hanoi problem * that uses command line arguments * * Usage: toh <positive integer> * where the integer is the number of disks ************************************************************/ #include <stdio.h> #include <stdlib.h> void TowersOfHanoi(int n, char from, char to, char spare); int main (int argc, char **argv) { int n; if (argc != 2) { printf("Usage: toh <positive integer>\n"); exit(-1); } n = atoi(argv[1]); printf("Moving %d discs from peg %c to peg %c:\n", n, 'A', 'B'); TowersOfHanoi(n, 'A', 'B', 'C'); return 0; } void TowersOfHanoi (int n, char from, char to, char spare) { if (n < 1) { return; } TowersOfHanoi(n-1, from, spare, to); printf(" move a disc from peg %c to peg %c\n", from, to); TowersOfHanoi(n-1, spare, to, from); }

%gcc -o toh -Wall -ansi toh.c%toh 3Moving 3 discs from peg A to peg B: move a disc from peg A to peg B move a disc from peg A to peg C move a disc from peg B to peg C move a disc from peg A to peg B move a disc from peg C to peg A move a disc from peg C to peg B move a disc from peg A to peg B %toh 4Moving 4 discs from peg A to peg B: move a disc from peg A to peg C move a disc from peg A to peg B move a disc from peg C to peg B move a disc from peg A to peg C move a disc from peg B to peg A move a disc from peg B to peg C move a disc from peg A to peg C move a disc from peg A to peg B move a disc from peg C to peg B move a disc from peg C to peg A move a disc from peg B to peg A move a disc from peg C to peg B move a disc from peg A to peg C move a disc from peg A to peg B move a disc from peg C to peg B %toh 5Moving 5 discs from peg A to peg B: move a disc from peg A to peg B move a disc from peg A to peg C move a disc from peg B to peg C move a disc from peg A to peg B move a disc from peg C to peg A move a disc from peg C to peg B move a disc from peg A to peg B move a disc from peg A to peg C move a disc from peg B to peg C move a disc from peg B to peg A move a disc from peg C to peg A move a disc from peg B to peg C move a disc from peg A to peg B move a disc from peg A to peg C move a disc from peg B to peg C move a disc from peg A to peg B move a disc from peg C to peg A move a disc from peg C to peg B move a disc from peg A to peg B move a disc from peg C to peg A move a disc from peg B to peg C move a disc from peg B to peg A move a disc from peg C to peg A move a disc from peg C to peg B move a disc from peg A to peg B move a disc from peg A to peg C move a disc from peg B to peg C move a disc from peg A to peg B move a disc from peg C to peg A move a disc from peg C to peg B move a disc from peg A to peg B %

- The Towers of Hanoi problem has a simple, easy to understand, recursive solution.
- This can be easily implemented as a recursive program.
- There are iterative solutions, but these are more obscure.
- The iterative solutions are not significantly more efficient.
- Solving the Towers of Hanoi puzzle with N discs requires 2**N-1 disc moves.
- The towers of Hanoi is a common example of a problem that is a good fit for a recursive solution.

CSEE | 201 | 201 F'06 | lectures | news | help