-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva101.cpp
More file actions
101 lines (84 loc) · 2.52 KB
/
uva101.cpp
File metadata and controls
101 lines (84 loc) · 2.52 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
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
class Piles {
private:
vector<vector<int>> pile;
vector<int> x,y;
void resetPosition(int n) {
int xx = x[n], yy = y[n];
for (int i = 1; pile[n][i] >= 0; i++) {
x[pile[n][i]] = pile[n][i];
y[pile[n][i]] = 0;
pile[n][i] = -1;
}
}
public:
Piles(int n) {
x.resize(n, 0);
y.resize(n, 0);
for(int i = 0; i< n; i++) {
pile.push_back(vector<int>(n+1,-1));
pile[i][0]= i;
x[i] = i;
}
}
void processCommand(string command, int from, string where, int to) {
if (command == "move" and where == "onto") {
resetPosition(from);
resetPosition(to);
pile[to][0] = to;
pile[to][1] = from;
} else if (command == "move" and where == "over") {
resetPosition(from);
int i = 0;
while(pile[to][i] >= 0) i++;
pile[to][i] = from;
} else if(command == "pile" and where == "onto") {
resetPosition(to);
for (int i = 0; pile[from][i] >= 0; i++) {
pile[to][i+1] = pile[from][i];
}
resetPosition(from);
} else if (command == "pile" and where == "over") {
int i = 0;
for(; pile[to][i] >= 0; i++);
for (int j = 0; pile[from][j] >= 0; j++) {
pile[to][i+j] = pile[from][j];
}
resetPosition(from);
}
}
void printResult(){
for (int i = 0; i < pile.size(); i++) {
cout << i <<":";
for(int j = 0; pile[i][j] >= 0; j++) {
cout << " " << pile[i][j];
}
cout << "\n";
}
}
};
int main() {
string s;
int n;
getline(cin,s);
istringstream iss(s);
iss >> n;
Piles pile(n);
while(true) {
getline(cin,s);
iss = istringstream(s);
cout << "debug " << s << "\n";
string command, from, where, to;
iss >> command;
if (command == "quit") {
break;
}
iss >> from >> where >> to;
pile.processCommand(command, stoi(from), where, stoi(to));
}
pile.printResult();
}