12906 - Tower of Hanoi   

Description

Tower of Hanoi is a mathematical puzzle where we have three rods and N disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:

  1. Only one disk can be moved at a time.
  2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
  3. No disk may be placed on top of a smaller disk.

Given a integer N to represents N disks. Please write a C program to show all the steps to move all N disks from rod 'A' to rod 'C'.

 

Input

An integer N to represent the number of disks.

Note that:

  1. 1 <= N <= 16

Output

Output should follow below format:

Disk n moved from X to Y

Note that:

  1. Need to have a return value('\n') at the end of your string.
  2. n is the number of moving disk.
  3. X, Y are one of ‘A’, ‘B’, ‘C’, which represents the index of rod. ‘A’ is the first rod, ‘B’ is the second rod, and ‘C’ is the third rod.
 

Sample Input  Download

Sample Output  Download

Tags




Discuss