-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-101.cpp
151 lines (139 loc) · 2.34 KB
/
UVA-101.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<stdio.h>
#include<vector>
#include<iostream>
#include<string>
using namespace std;
vector<int> v[26];
int n;
struct pos {
int x, y;
};
void show() {
int i, j;
for(i = 0; i < n; ++i) {
printf("%d:", i);
for(j = 0; j < v[i].size(); ++j) {
printf(" %d", v[i][j]);
}
putchar('\n');
}
}
pos findpos(int a) {
pos p = {0, 0};
int i, j;
for(i =0; i < n; ++i) {
for(j = 0; j < v[i].size(); ++j) {
if(v[i][j] == a) {
p.x = i;
p.y = j;
return p;
}
}
}
return p;
}
bool inipos(pos p, int bx) {
int i, j;
bool bb = false;
vector<int> vp = v[p.x];
v[p.x].resize(p.y);
for(i = p.y+1; i < vp.size(); ++i) {
if(vp[i] == bx) {
bb = true;
continue;
}
v[vp[i]].push_back(vp[i]);
}
return bb;
}
void moveonto(int a, int b) {
pos pa = findpos(a);
pos pb = findpos(b);
if(pa.x == pb.x) {
return;
}
bool ba = inipos(pa, pb.x), bb = inipos(pb, pb.x);
bb = ba && bb;
v[pb.x].push_back(b);
v[pb.x].push_back(a);
if(bb) {
v[pb.x].push_back(pb.x);
}
}
void moveover(int a, int b) {
pos pa = findpos(a);
pos pb = findpos(b);
if(pa.x == pb.x) {
return;
}
bool bb = inipos(pa, pb.x);
v[pb.x].push_back(a);
if(bb) {
v[pb.x].push_back(pb.x);
}
}
void pileonto(int a, int b) {
int i;
pos pa = findpos(a);
pos pb = findpos(b);
if(pa.x == pb.x) {
return;
}
vector<int> vp = v[pb.x];
v[pb.x].resize(pb.y + 1);
for(i = pa.y; i < v[pa.x].size(); ++i) {
v[pb.x].push_back(v[pa.x][i]);
}
v[pa.x].resize(pa.y);
for(i = pb.y+1; i < vp.size(); ++i) {
v[vp[i]].push_back(vp[i]);
}
}
void pileover(int a, int b) {
int i;
pos pa = findpos(a);
pos pb = findpos(b);
if(pa.x == pb.x) {
return;
}
for(i = pa.y; i < v[pa.x].size(); ++i) {
v[pb.x].push_back(v[pa.x][i]);
}
v[pa.x].resize(pa.y);
}
int main() {
int a, b;
int i, j, k;
string line, word1, word2;
while(scanf("%d", &n) == 1) {
for(i = 0; i < n; ++i) {
v[i].clear();
v[i].push_back(i);
}
while(1) {
cin >> word1;
if(word1 == "quit") {
break;
}
cin >> a >> word2 >> b;
if(a == b) {
continue;
}
if(word1 == "move" && word2 == "onto") {
moveonto(a, b);
}
if(word1 == "move" && word2 == "over") {
moveover(a, b);
}
if(word1 == "pile" && word2 == "onto") {
pileonto(a, b);
}
if(word1 == "pile" && word2 == "over") {
pileover(a, b);
}
// show();
}
show();
}
return 0;
}