#include <stdio.h>


char hanoi_buf[3][100];
int each_num[3];

int steps = 0;
int numbers;

void init_hanoi_buf(int n)
{
int i = 0;
numbers = n;

for(i =0;i < n; i++)
{
hanoi_buf[0][i] = n - i;
}


each_num[0] = n;
}

void print_hanoi_buf(void)
{
int i = 0;
int j = 0;

printf("__________________step:%d___________________\n", steps);
for (i = numbers - 1; i >= 0; i--)
{

for(j = 0; j < 3; j++)
{
if(/*each_num[j] >= i &&*/ hanoi_buf[j][i] !=0 )
printf("%d ", hanoi_buf[j][i]);
else
printf(" ");
}

printf("\n");
}

printf("A B C\n");
printf("_____________________________________\n");
}

#define IDX(x) (x-'A')
/*
a is org
c is target
b is temp.
*/
void hanoi(int n, char a, char b, char c)
{
if(n == 1)
{
printf("%s [%d]%d %c -->%c\n", __FUNCTION__, __LINE__, n , a, c);
steps++;
hanoi_buf[IDX(c)][each_num[IDX(c)]] = hanoi_buf[IDX(a)][each_num[IDX(a)]-1];
hanoi_buf[IDX(a)][each_num[IDX(a)]-1] = 0;
each_num[IDX(c)]++;
each_num[IDX(a)]--;
print_hanoi_buf();
}
else
{
hanoi(n - 1, a, c, b);
printf("%s [%d]%d %c -->%c\n", __FUNCTION__, __LINE__, n , a, c);

steps++;
hanoi_buf[IDX(c)][each_num[IDX(c)]] = hanoi_buf[IDX(a)][each_num[IDX(a)]-1];
hanoi_buf[IDX(a)][each_num[IDX(a)]-1] = 0;
each_num[IDX(c)]++;
each_num[IDX(a)]--;

print_hanoi_buf();

hanoi(n - 1, b, a, c);
}
}
int main()
{
int n = 3;

printf("_____________________________________\n");
init_hanoi_buf(n);
print_hanoi_buf();

hanoi(n, 'A', 'B', 'C');
return 0;
}