treesummaryrefslogcommitdiff
path: root/hanoi.c
blob: ddbe4db9c98ecfb376d2fcb0a0b33126f21e7ccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <stdlib.h>

void hanoi(char from, char to, int disks)
{
    char third = ('a' + 'b' + 'c') - from - to;

    if (disks == 1)
    {
        printf("%c -> %c\n", from, to);
    }
    else
    {
        hanoi(from, third, disks - 1);
        printf("%c -> %c\n", from, to);
        hanoi(third, to, disks - 1);
    }
}

int main(int argc, char **argv)
{
    if (argc != 4)
    {
        printf("Usage: %s <from> <to> <disks>\n", argv[0]);
        return 1;
    }

    char from = argv[1][0];
    char to = argv[2][0];
    int disks = atoi(argv[3]);

    hanoi(from, to, disks);

    return 0;
}