Ejemplo n.º 1
0
void towerOfHanoi(short height, short from, short to, short with) { // height = disks
    if (height > 0) {
        towerOfHanoi(height - 1, from, with, to);
        
        printf("Move the disk from %d to %d.\n", from, to);
        towerOfHanoi(height - 1, with, to, from);
    }
}
Ejemplo n.º 2
0
void towerOfHanoi(int n, char fromrod, char torod, char auxrod)
{
    if (n == 1)
    {
        printf("\n Move disk 1 from rod %c to rod %c", fromrod, torod);
        return;
    }
    towerOfHanoi(n-1, fromrod, auxrod, torod);
    printf("\n Move disk %d from rod %c to rod %c", n, fromrod, torod);
    towerOfHanoi(n-1, auxrod, torod, fromrod);
}
Ejemplo n.º 3
0
void towerOfHanoi(std::stack<int> *a, std::stack<int> *buff, std::stack<int> *dest, int n) {
	if (n == 0) {
		return;
	}
	// Moving disks, using destination as a buffer
	towerOfHanoi(a, dest, buff, n-1);
	(*dest).push((*a).top());
	(*a).pop();
	// Moving from our buffer to the destination
	towerOfHanoi(buff, a, dest, n-1);
}
Ejemplo n.º 4
0
int main() {
	std::stack<int> primary, buffer, destination;
	int n = 10;
	for (int i = n; i > 0; i--) {
		std::cout << i << " ";
		primary.push(i);
	}
	towerOfHanoi(&primary, &buffer, &destination, 10);
	std::cout << std::endl;
	while(!destination.empty()) {
		std::cout << destination.top() << " ";
		destination.pop();
	}
}
Ejemplo n.º 5
0
int main()
{
    int n = 4; // Number of disks
    towerOfHanoi(n, 'A', 'C', 'B');  // A, B and C are names of rods
    return 0;
}
Ejemplo n.º 6
0
int main()
{
    int n = 4; 
    towerOfHanoi(n, 'A', 'C', 'B');  
    return 0;
}