very hard for me what is the way to solv ??

Discussion in 'C Programming' started by wahid, Jan 10, 2010.

  1. wahid

    wahid Guest

    • The Tower of Hanoi or Towers of Hanoi is a
    mathematical game or puzzle. It consists of three
    rods, and a number of disks of different sizes
    which can slide onto any rod.
    • The objective of the puzzle is to move the entire
    stack to another rod, obeying the following rules:
    – Only one disk may be moved at a time.
    – Each move consists of taking the upper disk from one
    of the rods and sliding it onto another rod, on top of
    the other disks that may already be present on that
    rod.
    – No disk may be placed on top of a smaller disk.

    • Using recursion, write a program, that would
    solve the tower of Hanoi Problem.
    • Inputs: Number of Disks, Name of the three
    pegs ( Peg on which initially all the disks rest is
    input peg, peg on which finally all disks will lie
    is output peg, the third peg is the other peg)

    (Hint: think recursively, what if there were only two disks?
    and what if the N-1 small disks where combined into a single disk?)

    Sample Output:

    Enter Input peg: 1
    Enter output peg :2
    Enter other peg : 3
    Enter the number of disks :3
    Tower of hanoi Solution :
    Move Disk from Peg 1 to Peg 2
    Move Disk from Peg 1 to Peg 3
    Move Disk from Peg 2 to Peg 3
    Move Disk from Peg 1 to Peg 2
    Move Disk from Peg 3 to Peg 1
    Move Disk from Peg 3 to Peg 2
    Move Disk from Peg 1 to Peg 2
     
    wahid, Jan 10, 2010
    #1
    1. Advertising

  2. On 10 Jan 2010 at 7:15, wahid wrote:
    > • Using recursion, write a program, that would solve the tower of
    > Hanoi Problem.
    > • Inputs: Number of Disks, Name of the three pegs ( Peg on which
    > initially all the disks rest is input peg, peg on which finally all
    > disks will lie is output peg, the third peg is the other peg)


    #include <stdio.h>
    #include <stdlib.h>

    int get_positive_number(const char *prompt)
    {
    int d;
    fputs(prompt, stdout);
    fflush(stdout);
    if(scanf("%d", &d) == 1 && d > 0)
    return d;
    fputs("Invalid input: needed a positive integer\n", stderr);
    exit(1);
    }

    void hanoi(int n, int x, int y, int z)
    {
    if(n) {
    hanoi(n - 1, x, z, y);
    printf("Move disk from peg %d to peg %d\n", x, y);
    hanoi(n - 1, z, y, x);
    }
    }

    int main(void)
    {
    int x, y, z, n;
    x = get_positive_number("Enter input peg: ");
    y = get_positive_number("Enter output peg: ");
    z = get_positive_number("Enter other peg: ");
    n = get_positive_number("Enter the number of disks: ");
    hanoi(n, x, y, z);
    return 0;
    }
     
    Antoninus Twink, Jan 10, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    525
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,730
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    475
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    549
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,384
Loading...

Share This Page