Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Tower of Hanoi (See related posts)

Solves the old "Tower of Hanoi" problem using a recursive divide et impera approach.

http://en.wikipedia.org/wiki/Tower_of_Hanoi

#include <stdio.h>

void hanoi(int n, char a, char b, char c) {
	if (!n)	
		return;
	
	hanoi(n - 1, a, c, b);
	printf("%c -> %c\n", a, b);
	hanoi(n - 1, c, b, a);
}

int main(int argc, char *argv[]) {
	int n = 3;
	hanoi(n, 'a', 'b', 'c');
	
	return 0;
}

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts