Untitled
unknown
plain_text
3 years ago
2.1 kB
10
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n, table[25][25], book[25], from , to, amount[25];
char oper1[8], oper2[8];
void re(int tab, int from), pile(int tabf, int tabt);
int main(){
scanf("%d", &n);
for(int i = 0; i < n; i++){
table[i][0] = i, book[i] = i;
amount[i]++;
}
while(scanf("%s", oper1)){
int stop = strcmp(oper1, "exit");
if(!stop) break;
scanf("%d %s %d", &from, oper2, &to);
int move = strcmp(oper1, "move");
int onto = strcmp(oper2, "onto");
int ft = book[from], tt = book[to];
if(ft == tt) continue;
if(!move && !onto){
re(ft, from);
re(tt, to);
table[ft][amount[ft] - 1] = 0, table[tt][amount[tt]] = from;
book[ft] = tt;
amount[ft]--, amount[tt]++;
}else if(!move && onto != 0){
re(ft, from);
table[ft][amount[ft] - 1] = 0, table[tt][amount[tt]] = from;
book[ft] = tt;
amount[ft]--, amount[tt]++;
}else if(move != 0 && !onto){
re(tt, to);
pile(ft, tt);
}else if(move != 0 && onto != 0){
pile(ft, tt);
}
}
for(int i = 0; i < n; i++){
printf("%d:", i);
for(int j = 0; j < amount[i]; j++) printf(" %d", table[i][j]);
printf("\n");
}
return 0;
}
void re(int tab, int from){
for(int i = amount[tab] - 1; table[tab][i] != from; i--){
int num = table[tab][i];
table[tab][i] = 0, table[num][amount[num]] = num;
book[num] = num;
amount[tab]--, amount[num]++;
}
return;
}
void pile(int tabf, int tabt){
int count = 0, ok = 0;
for(int i = 0; i < amount[tabf]; i++){
int num = table[tabf][i];
if(table[tabf][i] == from) ok = 1;
if(ok == 1){
table[tabf][i] = 0, table[tabt][amount[tabt]] = num;
book[num] = tabt;
amount[tabt]++, count++;
}
}
amount[tabf] -= count;
}Editor is loading...