11199 - Tower of Hanoi   

Description

The Tower of Hanoi is a mathematical game or puzzle. It consists of 3 rods, and N disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.

The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:

  1. Only 1 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.

In this problem, you are given 3 rods named rod 1, 2 and 3, with N disks in a stack onto rod 1. Your mission is to move all disks onto disk 2 in minimal steps without breaking the riles provided. You have to print all moves.

You only have to implement the function "hanoi()" to achive the goal. Here is the defination of hanoi():

void hanoi( int rod_N, int rod_A, int rod_B, int rod_C ) ;

rod_N is the number of disks to be move right now, while rod_A, _B, and _C denoted the rod ID, which should be 1, 2, or 3. The function is to "Move the top rod_N disks from rod_A to rod_B via rod_C", and our main() function will hanoi(N, 1, 2, 3) without printing anything.

 

Input

The input consists of only one positive integers, denoting there are N disks given. At most 8 disks may be given.

Output

For the input, print all required steps of moving. Each move should be printed as an one-lined message like "Move from rod 1 to rod 2.\n". Please replace the rod ID on your own.

Sample Input  Download

Sample Output  Download

Partial Judge Code

11199.c

Partial Judge Header

11199.h

Tags




Discuss