-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuva-101.cpp
More file actions
171 lines (119 loc) · 3.31 KB
/
uva-101.cpp
File metadata and controls
171 lines (119 loc) · 3.31 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//uva 101
//The Blocks Problem
#include <iostream>
#include <string>
using namespace std;
//prototypes
void returnBlocks(int **arr, int *currents, int n, int num);
void put(int ** arr, int *currents, int n, int num, int place);
int main(void)
{
int n = 0;
cin >> n;
int ** arr = new int *[n]();
*arr = new int[n * n]();
for (int i = 0; i < n; ++i)
arr[i] = (*arr + i * n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
arr[i][j] = -1;
for (int i = 0; i < n; ++i)
arr[i][0] = i;//initial place for blocks
int * currents = new int[n]();
for (int i = 0; i < n; ++i)
currents[i] = i;
while (true) {
string part1;
string part2;
cin >> part1;
if (part1 == "quit")
break;
int a = 0, b = 0;
cin >> a >> part2 >> b;
if (part1 == "move" && currents[a] != currents[b]) {
if (part2 == "onto") {
returnBlocks(arr, currents, n, a);
returnBlocks(arr, currents, n, b);
int counter = 0;
while (arr[currents[a]][counter] != a)
++counter;
arr[currents[a]][counter] = -1;//removing a from previous location
put(arr, currents, n, a, currents[b]);
}
else if (part2 == "over") {
returnBlocks(arr, currents, n, a);
int counter = 0;
while (arr[currents[a]][counter] != a)
++counter;
arr[currents[a]][counter] = -1;//removing a from previous location
put(arr, currents, n, a, currents[b]);
}
}
else if (part1 == "pile" && currents[a] != currents[b]) {
if (part2 == "onto") {
returnBlocks(arr, currents, n, b);
int col = 0;
while (arr[currents[a]][col] != a)
++col;
int row = currents[a];
while (col < n && arr[row][col] != -1) {
put(arr, currents, n, arr[row][col], currents[b]);
arr[row][col] = -1;
++col;
}
}
else if (part2 == "over") {
int col = 0;
while (arr[currents[a]][col] != a)
++col;
int row = currents[a];
while (col < n && arr[row][col] != -1) {
put(arr, currents, n, arr[row][col], currents[b]);
arr[row][col] = -1;
++col;
}
}
}
}//end while
for (int i = 0; i < n; ++i) {
cout << i << ':';
for (int j = 0; j < n; ++j)
if (arr[i][j] != -1)
cout << ' ' << arr[i][j];
cout << endl;
}
delete[] * arr;
*arr = nullptr;
delete[] arr;
arr = nullptr;
delete[] currents;
currents = nullptr;
return 0;
}
void put(int ** arr, int *currents,int n, int num, int place)//function for puting an element at the end of a block
{ //num is always at the end of the column
int counter = 0;
/*while (counter < n && arr[currents[num]][counter] != num)
++counter;
if (counter < n)
arr[currents[num]][counter] = -1;//removing from its current place*/
counter = 0;
while (counter < n && arr[place][counter] != -1)
++counter;
arr[place][counter] = num;
currents[num] = place;
return;
}
void returnBlocks(int **arr, int *currents, int n, int num)
{
for (int i = n - 1; i >= 0; --i) {
if (arr[currents[num]][i] == num)
return;
if (arr[currents[num]][i] != -1) {
int previous_location = currents[num];
put(arr, currents, n, arr[currents[num]][i], arr[currents[num]][i]);
arr[previous_location][i] = -1;
}
}
return;
}