follow 題目的規定操作即可。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
int amount,num1, num2;;
char c1[10] = {0}, c2[10] = {0};
int table[25]={0},a[25][25],top[25], prepare[25];
int target, order, pre_top;
int i,j, temp;
char c;
while(scanf("%d", &amount)!=EOF){
for(i=0;i<amount;i++){
a[i][0]=i;
table[i]=i;
top[i]=0;
}
while(1){
scanf("%s", &c1);
if(strcmp(c1,"quit")==0)
break;
scanf(" %d %s %d%c",&num1,&c2,&num2,&c);
if( (num1==num2) || (table[num1]==table[num2]) )
continue;
if(strcmp(c1,"move")==0){
for(temp = top[table[num1]] ; a[ table[num1] ][ temp ] != num1 ; temp--){
target = a[ table[num1] ][ temp ];
order = top[ target ] + 1;
top[ target ]= order;
a[target][order]=target;
table[target]=target;
}
top[ table[num1] ] = temp-1;
if(strcmp(c2,"onto")==0){
for(temp = top[table[num2]] ; a[ table[num2] ][ temp ] != num2 ; temp--){
target = a[ table[num2] ][ temp ];
order = top[ target ] + 1;
top[ target ]= order;
a[target][order]=target;
table[target]=target;
}
top[ table[num2] ] = temp + 1;
order = top[ table[num2] ];
a[ table[num2] ][order] = num1;
}else if(strcmp(c2,"over")==0){
++top[ table[num2] ];
order = top[ table[num2] ];
a[ table[num2] ][order] = num1;
}
table[num1]=table[num2];
}else if(strcmp(c1,"pile")==0){
for(i=0; a[ table[num1] ][i] != num1 ;i++);
temp = i-1;
for(pre_top=-1;i<=top[ table[num1] ];i++)
prepare[++pre_top]=a[ table[num1] ][i];
top[ table[num1] ] = temp;
if(strcmp(c2,"onto")==0){
for(temp = top[table[num2]] ; a[ table[num2] ][ temp ] != num2 ; temp--){
target = a[ table[num2] ][ temp ];
order = top[ target ] + 1;
top[ target ]= order;
a[target][order]=target;
table[target]=target;
}
top[ table[num2] ] = temp;
}
for(order=top[ table[num2] ], i=0 ; i<=pre_top ; i++){
a[table[num2]][++order] = prepare[i];
table[prepare[i]] = table[num2];
}
top[table[num2]] = order;
}
}
for(i=0;i<amount;i++){
printf("%d:",i);
for(j=0;j<=top[i];j++)
printf(" %d",a[i][j]);
printf("\n");
}
}
return 0;
}
沒有留言:
張貼留言