|
#define MAX 300
#include<stdio.h>
struct state_style{
int num;
char target;
char middle;
char source;};
struct state_style stack[MAX];
int s,top;
void set_state(int num,char x,char y,char z,struct state_style *state)
{
state->num=num;
state->source=x;
state->middle=y;
state->target=z;
}
void push(struct state_style state)
{
stack[top++]=state;
}
void pop(struct state_style *state)
{
*state=stack[--top];
}
int empty()
{
if (top==0) return 1;
return 0;
}
void han(int n)
{
struct state_style state;
char temp;
set_state(n,'x','y','z',&state);
do{
if (state.num>0)
{
push(state);
set_state(state.num-1,state.source,state.target,state.middle,&state);
}
else {
pop(&state);
printf("%d: Move %d from %c to %c.\n",++s,state.num,state.source,state.target);
set_state(state.num-1,state.middle,state.source,state.target,&state);
}
}while(!((state.num==0)&&empty()));
}
int main()
{
int n;
scanf("%d",&n);
han(n);
} |
|