#include <stdio.h>
#include <string.h>

#define MAXN 50

struct struct_pilha {
  int disks[MAXN];
  int top;
  char name;
};

typedef struct struct_pilha *pilha;

pilha a, b, c; /* apontadores para as 3 pilhas */
int move_number=0;
int verbose=0; /* flag for verbose mode */

int scan_command(int argc, char *argv[], int *n, int *verbose)
{
  while (--argc) {
    if (sscanf(argv[argc],"-n%lu",n)==1) ;
    else if (strcmp(argv[argc],"-v")==0) *verbose=1;
    else {
      printf("Usage: %s [-nN][-v]\n", argv[0]);
      return -1;
    }
  }
  return 0; /* normal exit from ths function */
}

void print_pilha(pilha v)
{
  int i;
  printf("\n %c: ", v->name);
  for (i=0; i<=v->top; i++)
    printf(" %d", v->disks[i]);
}

void print_situation()
{
  print_pilha(a);
  print_pilha(b);
  print_pilha(c);
  printf("\n");
}

pilha aloca(void)
{ return (pilha)malloc(sizeof(struct struct_pilha)); }

void set_up_start(int n)
{
  int i;
  
  a=aloca();
  b=aloca();
  c=aloca();
  for (i=0; i<n; i++)
    a->disks[i]=i;
  a->top=n-1; a->name='A';
  b->top=-1; b->name='B';
  c->top=-1; c->name='C';
}

void solve(pilha u, pilha v, pilha w, int n_disks)
{
  if (n_disks==1) {
    v->disks[++v->top]=u->disks[u->top--];
    printf("Move number %d: disk %d, %c -> %c\n", 
      ++move_number, v->disks[v->top], u->name, v->name);
    if (verbose) print_situation();
  } else {
    solve(u, w, v, n_disks-1);
    v->disks[++v->top]=u->disks[u->top--];
    printf("Move number %d: disk %d, %c -> %c\n", 
      ++move_number, v->disks[v->top], u->name, v->name);
    if (verbose) print_situation();
    solve(w, v, u, n_disks-1);
  }
}

main(argc,argv)
  int argc; /* the number of command-line arguments */
  char *argv[]; /* an array of strings containing those arguments */
{
  int n=3; /* number of disks */

  if (scan_command(argc, argv, &n, &verbose)==0) {
    set_up_start(n);
    print_situation();
    solve(a, b, c, n);
    print_situation();
    return 0; /* normal exit */
  }
}
   


