# Thread: Tower of Hanoi Alteration Help!!

1. ## Tower of Hanoi Alteration Help!!

I'm having some problem with this problem. It is an alteration to the towers of hanoi problem and I have no idea how to implement it. Here's what I have to do:

The Towers of HaHa problem is the same as the Towers of Hanoi problem. However, the disks are numbered 1 through n; odd-numbered disks are red, and even-numbered ones are yellow. The disks are initially on tower 1 in the order 1 through n from top to bottom. The disks are to be moved to tower 2, and at no time should a disk sit on top of a disk that has the same color. The initial and final disk orders are the same. Write a program to move the disks from tower 1 to tower 2 using tower 3 for intermediate storage. How many disk moves does your program make?

So I started by creating the towers of hanoi problem and I have no idea where to check that they're not the same color when you move them:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>

#define FROM	1
#define TO	3
#define USING	2

void dohanoi(int N, int from, int to, int using)
{
if (N > 0) {
dohanoi(N-1, from, using, to);
if ((N % 2) == 0) {
printf ("move Blue %d --> %d\n", from, to);
} else {
printf ("move Red %d --> %d\n", from, to);
}
dohanoi(N-1, using, to, from);
}
}

int main (int argc, char **argv)
{
long int N;

if (argc != 2) {
fprintf(stderr, "usage: %s N\n", argv[0]);
exit(1);
}

N = strtol(argv[1], (char **)NULL, 10);

/* a bit of error checking, LONG_XXX should be there in limits.h */
if (N == LONG_MIN || N == LONG_MAX || N <= 0) {
fprintf(stderr, "illegal value for number of disks\n");
exit(2);
}

dohanoi(N, FROM, TO, USING);

exit(0);
}```

2. Ignore the "color". Just check if they are even or odd.

Quzah.

3. So like, if N is even and to is even obviously they cannot be stacked so how would I change where it's being moved to?

4. Well it's not like you have many choices. You have three pegs. You have a source peg, and two destination pegs. If it can't go on one, what's that leave you?

Quzah.

5. Do you have to work with the values of red and yellow as your requirements? Are you then familiar with enumerations. If you are only working with odd or even, then taking the modulus pretty much shrinks that down to 0 and 1 anyways.

enum color = { red, yellow};

6. How exactly would I check the value of the disc on the stack that I'm moving the current disc to?

How exactly would I check the value of the disc on the stack that I'm moving the current disc to?
Forget about any coding here...have you played ToH? Can you write the logistics down in pseudocode? How would you mentally figure out what the size/color is that you are about to place down on for each peg?

Each Peg, can, essentially represent its own array, keeping track in order, bottom to top, of its values being 0 for red and 1 for yellow.

8. Okay, so if I have an array for each peg:

source[1], dest[1], intermediate[1];

When I move a pole to a destination, I update that pole's array with either 0 or 1 depending on whether it's even or odd. I can then always know what's on top of each pole before I move the next disc. This should work then, right?

9. Stop. Go out and buy yourself one of those Fischer-Price pegs with the colored rings. Actually, go buy three of them, and throw two sets of rings away. Then figure out how to play Hanoi with it until you understand what's going on. Until you understand the towers, there's no point in trying to write it. (As you've already been told.)

Quzah.

10. I understand how to play and how the game works. I'm sorry if it's hard for me to phrase things. I don't know what else I can do.

11. Code:
```int peg1[ MAXRINGS ];
int peg2[ MAXRINGS ];
int peg3[ MAXRINGS ];```
Make three pegs, each one is big enough to hold all of your rings. Fill one of them with all the rings in order. Now move them around until they're all in order some place else.

Quzah.

12. Is it absolutely necessary to utilize the arrays like that? Why would I not want to just keep track of the ring on the top of each peg? It seems like there could be issues the larger n gets.

13. ## .

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 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 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.
right theres the rules above.

In the example you were shown last you can see that the arraysize is specified [maxrings], ie space is allocated so that the max rings in your game can be stored there, thems the rules, you start with a fixed number of rings, like five, so your arrays only have to be five elements in size, it does make sense to have the arrays as shown, its a doddle to access the values representing your rings and it will do what you want so yes i say its an ideal solution for how to represent your rings and poles.

with 5 being the biggest ring and 1 being the smallest, your start position might look like this in memory >

Code:
```int pole_A[5] = {5, 4, 3, 2, 1};
int pole_B[5] = { 0, 0, 0, 0 ,0 };
int pole_C[5] = { 0, 0, 0, 0 ,0 };```
then off you go moving them around, recursively, whatever.

14. Okay, so I'm on my way. I've initialized the peg1 array based on the number of rings the user inputs. Would it make more sense to use a 2-dimensional array peg[1][maxRings] peg[2][maxRings] peg[3][maxRings]

Code:
```#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

int peg1[], peg2[], peg3[];

void hanoi(int n,int from,int using,int to) {

if (n==1)
{
printf("Move from %d to %d.\n",from,to);
} else {

hanoi(n-1,from,to,using);
hanoi(1,from,using,to);
hanoi(n-1,to,using,from);
}
}

int main(int argc, char **argv) {
int maxRings = strtol(argv[1], (char **)NULL, 10);
int i, j, peg1[maxRings], peg2[maxRings], peg3[maxRings];

for (j = 0; j < maxRings; j++) {
for (i = maxRings-j; i>0; i--) {
peg1[j] = i;
break;
}
}
hanoi(4,1,3,2);
exit(0);
}```

15. No. One dimension is preferred.