-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101sol.cpp
More file actions
115 lines (99 loc) · 1.92 KB
/
101sol.cpp
File metadata and controls
115 lines (99 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<stdio.h>
#include<string.h>
#define MAXN 30
char Com1[50], Com2[50];
int N, A, B;
struct ss {
int stack[MAXN];
int index;
int add;
int pos;
}Block[MAXN];
void Ini() {
int i;
for(i = 0; i<N; i++) {
Block[i].stack[0] = i;
Block[i].add = i;
Block[i].pos = 0;
Block[i].index = 1;
}
}
void Move_onto(int a) {
int i, j, k;
int add = Block[a].add;
int in = Block[add].index;
int p = Block[a].pos;
for(i = in-1; i>p; i--) {
j = Block[add].stack[i];
k = Block[j].index;
Block[j].add = j;
Block[j].stack[k++] = j;
Block[j].pos = k-1;
Block[j].index++;
Block[add].index--;
}
}
void Set() {
int ada, adb, in;
ada = Block[A].add;
adb = Block[B].add;
in = Block[adb].index;
Block[ada].index--;
Block[adb].stack[in++] = A;
Block[adb].index++;
Block[A].add = adb;
Block[A].pos = Block[adb].index-1;
}
void Pile() {
int posa, ada, adb, inda, indb;
int i, j;
ada = Block[A].add;
adb = Block[B].add;
posa = Block[A].pos;
inda = Block[ada].index;
indb = Block[adb].index;
for(i = posa; i<inda; i ++){
j = Block[ada].stack[i];
Block[adb].stack[indb++] = j;
Block[ada].index--;
Block[j].pos = indb-1;
Block[j].add = adb;
Block[adb].index++;
}
}
void Print() {
int i, j, index;
for(i = 0; i<N; i++) {
index = Block[i].index;
printf("%d:",i);
for(j = 0; j<index; j++)
printf(" %d",Block[i].stack[j]);
printf("\n");
}
}
void Cal() {
while(1) {
scanf("%s",Com1);
if(!strcmp(Com1,"quit")) break;
scanf("%d%s%d",&A,Com2,&B);
if(A == B || (Block[A].add == Block[B].add))
continue;
if(!strcmp(Com2,"onto"))
Move_onto(B);
if(!strcmp(Com1,"move")){
Move_onto(A);
Set();
continue;
}
if(!strcmp(Com1,"pile")){
Pile();
}
}
}
int main() {
while(scanf("%d",&N) == 1) {
Ini();
Cal();
Print();
}
}