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); }