Problem Definition
I'm trying write a c++ program to solve the Extended Hanoi Tower
problem.
Extended Hanoi Tower is similar to the standard hanoi problem. The difference is that, odd rings are in A (1, 3, 5, ...) and even rings are in B (2, 4, 6, ...). The question asks, how can we transfer all of the rings to C in a recursive way?
I've written what I've done so far.
Any help letting me implement my approach or Any ideas to implement the program would be appreciated !
Thanks !
Rules
1. Only one disk can be moved at a time.
2. No disk may be placed on top of a smaller disk.
3. The output should contain the necessary moves, not the minimum necessary moves
4. The total number of disks may be even or odd.
5. Implementation should be recursive.
Approach 1
we assume that the problem has been solved for n-1 rings that are currently in A and B. That means they are all moved to C and C have 2n-2 ordered rings. After that we have to handle the remaining rings in A & B. That means we have to take smaller ring and place it on the bigger ring (from B to A). After that we have to merge the rings in C (2n-2) and the rings in A (2). Finally we have to use standard Hanoi solution to reach our goal and move all of the rings to C.
Implementation 1
int main()
{
long int n;
cin >> n;
// move odd & even rings to the first shaft, recursively
customHanoi(n);
// move all rings from first shaft to the destination shaft, recursively
standardHanoi(n);
return 0;
}
// the shaft holds the odd rings: A
// the shaft holds the even rings: B
// the final destination shaft: C
void customHanoi(int n, char A, char B, char C)
{
if (n == 1)
return;
else if (n == 2)
{
cout << B << " " << A << endl;
return;
}
else if (n == 3)
{
cout << A << " " << C << endl;
cout << B << " " << A << endl;
cout << C << " " << A << endl;
}
else
{
// here we have some missing code !
}
}
// a recursive function to find the solution for standard hanoi
void standardHanoi(int n, char from, char helper, char to)
{
// the base condition
if (n == 1)
{
cout << from << " " << to << endl;
return;
}
// the recursive calls
standardHanoi(n - 1, from, to, helper);
cout << from << " " << to << endl;
standardHanoi(n - 1, helper, from, to);
}
Releated Resources
Link
n
should refer to number of disks in total. so, there's no2n-2
. – Will Nessn
is the number of disks in each rod. but my source code usedn
for the total number of disks in A & B. Thanks for your attention & Also for your quick response ! – HoseinGhanbari