Solved

HANOI TOWER

Posted on 1997-11-28
5
353 Views
Last Modified: 2008-02-26
Do you have hear about one game developed using  C++ programming called HANOI TOWER?. If you did, please give me the idea like algorithm or program structure about that problem.
0
Comment
Question by:feda
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1174326
move(int from, int to, int n){
  int other=from^to;
  if( n == 0 ){ return; }
  move(from,other,n-1);
  printf("move %d from %d to %d\n",n,from,to);
  move(other,to,n-1);
}
main(){
  move(1,2,5);
}

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1174327
ozo's code has the gist of it.

To explain, the idea is that you have 3 poles with a given number of disks, each disk being progresivley larger than the next (stacked like a pyramud).

You have to move all the diskd from the first pole to the third pole while keeping the following rules:

1. You can only move one disk ata time.

2.- You can never place a larger disk on top of a smaller disk.

3.- You can move any disk to any pole in any order.
0
 

Expert Comment

by:Zulqarnain
ID: 1174328
If u want code fragment and description, e-mail me at:
      runtime79@hotmail.com
0
 

Author Comment

by:feda
ID: 1174329
Still with the Hanoi tower, what is a C++ program for the algorithm below;
tower(positive integer n, peg i, peg j , peg k)
// move the top n disks on peg i to peg k using peg j
if n=1 then
  move top disk on peg i to peg k
else
  tower(n-1,i,k,j)
  move top diskk on peg i to peg k
  tower(n-1,j,i,k)

0
 
LVL 6

Accepted Solution

by:
jpk041897 earned 60 total points
ID: 1174330
The solution for C is the same as for C++, mainly:

void
move_tower(int n, post_t source, post_t dest, post_t tmp)
{
       if (num_disks == 1) {
              move1_disk(max_disks, source, dest);
              display_posts(max_disks);
       } else {
              move_tower(max_disks, num_disks - 1, source, tmp, dest);
              move1_disk(max_disks, source, dest);
              display_posts(max_disks);
              move_tower(max_disks, num_disks - 1, tmp, dest, source);
       }
}

You can place move tower inside a class, say x, in which case you would change the (untested) code to:

// x.h

typedef int post_t;

class x {
   private:
      int max_disks;
   public:
      x(); // write default constructor to define number of disks to a constant value, say 5.
      x(int numdisks){ this.max_disks = numdisks; }

      void move_tower( int n, post_t source, post_t dest, postT tmp);
      void move1_disk(max_disks, source, dest);
      void display_posts(max_disks);


// x.cpp
#include "x.h"

x :: move_tower(int n, post_t source, post_t dest, post_t tmp)
{
       if (num_disks == 1) {
              x.move1_disk(max_disks, source, dest);// write code to increment dest and decrement source by 1
              x.display_posts(max_disks); // write code to display the contents of eac post. (See example below)
       } else {
              x.move_tower(max_disks, num_disks - 1, source, tmp, dest);
              move1_disk(max_disks, source, dest);
              display_posts(max_disks);
              x.move_tower(max_disks, num_disks - 1, tmp, dest, source);
       }
}

// Place code for missing methods here.

Or just leave it as is in C and call it from main() since C code is perfectly valid in C++.


   |         |         |
   |         |         |
 *****       |         |
*******      *        ***
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now