-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaterjugpuzzle.cpp
More file actions
409 lines (337 loc) · 13.3 KB
/
Copy pathwaterjugpuzzle.cpp
File metadata and controls
409 lines (337 loc) · 13.3 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*******************************************************************************
* Name : waterjugpuzzle.cpp
* Author : Raqeebullah Hasan and Andrew Hing
* Date : 10-21-22
* Description : Solve water jug puzzle
* Pledge : I pledge my honor that I have abided by the Stevens Honor System.
******************************************************************************/
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
// Struct to represent state of water in the jugs.
struct State {
int a, b, c;
string directions;
State *parent;
State(int _a, int _b, int _c, string _directions)
: a{_a}, b{_b}, c{_c}, directions{_directions}, parent{nullptr} {}
// String representation of state in tuple form.
string to_string() {
ostringstream oss;
oss << "(" << a << ", " << b << ", " << c << ")";
return oss.str();
}
};
string display(stack<string>&result){
ostringstream oss;
// print the result
while (not result.empty()) {
oss << result.top(); // print the steps!!!
result.pop();
if(not result.empty())
oss << "\n";
}
return oss.str();
}
string bfs(int capA, int capB, int capC, int goalA, int goalB, int goalC) {
vector<vector<State *>> matrix(capA + 1, vector<State *>(capB + 1));
for (auto &rows : matrix) {
for (auto &columns : rows) {
columns = nullptr; // Initiliazing the array
}
}
State *initial = new State(0, 0, capC, "Initial");
State *current = new State(0, 0, capC, "Initial");
queue<State *> stateQueue;
stateQueue.push(current);
while (not stateQueue.empty()) {
current = stateQueue.front();
stateQueue.pop();
// Check if the goal is achieved: current == goal
if (current->a == goalA && current->b == goalB &&
current->c == goalC) { // Checkboard Flag
stack<string> result; // to store the pour result
State* latest = current;
while (current->directions != "Initial") {
result.push(current->directions); // push the directions until initial
current = current->parent; // climb back
}
current = latest;
delete current; // delete the current
result.push("Initial state. " + initial->to_string());
delete initial; // delete the intial
// deleting the queue
while (not stateQueue.empty()) {
delete stateQueue.front();
stateQueue.pop();
}
for (auto &rows : matrix)
for (auto &columns : rows)
delete columns; // deleting the array
return display(result);
} // Checkboard Flag
if(matrix[current->a][current->b] != nullptr){
delete current;
continue;
}else{
matrix[current->a][current->b] = current;
}
// Six Ways to pour water--------------------
// A into B starts
if ((current->a != 0) && (current->b != capB)) {
int quantityB = capB - current->b; // remaining quantity of jug B
State *newState = new State(current->a, current->b, current->c, "None");
string direction;
newState->parent = current;
if (quantityB >= current->a) { // if the a can be poured into b
newState->b += newState->a;
newState->a -= 0; // empty the jug A
if (current->a == 1)
direction += "Pour " + to_string(current->a) +
" gallon from A to B. " + newState->to_string();
else
direction += "Pour " + to_string(current->a) +
" gallons from A to B. " + newState->to_string();
} else { // Only pour the amount from A to fill the jug B
newState->b += quantityB;
newState->a -= quantityB;
if (quantityB == 1)
direction += "Pour " + to_string(quantityB) +
" gallon from A to B. " + newState->to_string();
else
direction += "Pour " + to_string(quantityB) +
" gallons from A to B. " + newState->to_string();
}
newState->directions = direction;
// Check if newState is already in the matrix
if (matrix[newState->a][newState->b] != nullptr)
delete newState;
else
stateQueue.push(newState);
} // A into B ends
// A into C starts
if ((current->a != 0) && (current->c != capC)) {
int quantityC = capC - current->c; // remaining quantity of jug C
State *newState = new State(current->a, current->b, current->c, "None");
string direction;
newState->parent = current;
if (quantityC >= current->a) { // if the a can be poured into c
newState->c += newState->a;
newState->a = 0; // empty the jug A
if (current->a == 1)
direction += "Pour " + to_string(current->a) +
" gallon from A to C. " + newState->to_string();
else
direction += "Pour " + to_string(current->a) +
" gallons from A to C. " + newState->to_string();
} else { // Only pour the amount from A to fill the jug C
newState->c += quantityC;
newState->a -= quantityC;
if (quantityC == 1)
direction += "Pour " + to_string(quantityC) +
" gallon from A to C. " + newState->to_string();
else
direction += "Pour " + to_string(quantityC) +
" gallons from A to C. " + newState->to_string();
}
newState->directions = direction;
// Check if newState is already in the matrix
if (matrix[newState->a][newState->b] != nullptr)
delete newState;
else
stateQueue.push(newState);
} // A into C ends
// B into A starts
if ((current->b != 0) && (current->a != capA)) {
int quantityA = capA - current->a; // remaining quantity of jug B
State *newState = new State(current->a, current->b, current->c, "None");
string direction;
newState->parent = current;
if (quantityA >= current->b) { // if the B jug can be poured into A
newState->a += newState->b;
newState->b = 0; // empty the jug B
if (current->b == 1)
direction += "Pour " + to_string(current->b) +
" gallon from B to A. " + newState->to_string();
else
direction += "Pour " + to_string(current->b) +
" gallons from B to A. " + newState->to_string();
} else { // Only pour the amount from B to fill the jug A
newState->a += quantityA;
newState->b -= quantityA;
if (quantityA == 1)
direction += "Pour " + to_string(quantityA) +
" gallon from B to A. " + newState->to_string();
else
direction += "Pour " + to_string(quantityA) +
" gallons from B to A. " + newState->to_string();
}
newState->directions = direction;
// Check if newState is already in the matrix
if (matrix[newState->a][newState->b] != nullptr)
delete newState;
else
stateQueue.push(newState);
} // B into A ends
// B into C starts
if ((current->b != 0) && (current->c != capC)) {
int quantityC = capC - current->c; // remaining quantity of jug C
State *newState = new State(current->a, current->b, current->c, "None");
string direction;
newState->parent = current;
if (quantityC >= current->b) { // if the C jug can be poured into A
newState->c += newState->b;
newState->b = 0; // empty the jug B
if (current->b == 1)
direction += "Pour " + to_string(current->b) +
" gallon from B to C. " + newState->to_string();
else
direction += "Pour " + to_string(current->b) +
" gallons from B to C. " + newState->to_string();
} else { // Only pour the amount from B to fill the jug C
newState->c += quantityC;
newState->b -= quantityC;
if (quantityC == 1)
direction += "Pour " + to_string(quantityC) +
" gallon from B to C. " + newState->to_string();
else
direction += "Pour " + to_string(quantityC) +
" gallons from B to C. " + newState->to_string();
}
newState->directions = direction;
// Check if newState is already in the matrix
if (matrix[newState->a][newState->b] != nullptr)
delete newState;
else
stateQueue.push(newState);
} // B into C ends
// C into A starts
if ((current->c != 0) && (current->a != capA)) {
int quantityA = capA - current->a; // remaining quantity of jug A
State *newState = new State(current->a, current->b, current->c, "None");
string direction;
newState->parent = current;
if (quantityA >= current->c) { // if the A jug can be poured into C
newState->a += newState->c;
newState->c = 0; // empty the jug C
if (current->c == 1)
direction += "Pour " + to_string(current->c) +
" gallon from C to A. " + newState->to_string();
else
direction += "Pour " + to_string(current->c) +
" gallons from C to A. " + newState->to_string();
} else { // Only pour the amount from C to fill the jug A
newState->a += quantityA; // full
newState->c -= quantityA;
if (quantityA == 1)
direction += "Pour " + to_string(quantityA) +
" gallon from C to A. " + newState->to_string();
else
direction += "Pour " + to_string(quantityA) +
" gallons from C to A. " + newState->to_string();
}
newState->directions = direction;
// Check if newState is already in the matrix
if (matrix[newState->a][newState->b] != nullptr)
delete newState;
else
stateQueue.push(newState);
} // C into A ends
// C into B starts
if ((current->c != 0) && (current->b != capB)) {
int quantityB = capB - current->b; // remaining quantity of jug A
State *newState = new State(current->a, current->b, current->c, "None");
string direction;
newState->parent = current;
if (quantityB >= current->c) { // if the B jug can be poured into C
newState->b += newState->c;
newState->c = 0; // empty the jug C
if (current->b == 1)
direction += "Pour " + to_string(current->b) +
" gallon from C to B. " + newState->to_string();
else
direction += "Pour " + to_string(current->b) +
" gallons from C to B. " + newState->to_string();
} else { // Only pour the amount from C to fill the jug B
newState->b += quantityB; // full
newState->c -= quantityB;
if (quantityB == 1)
direction += "Pour " + to_string(quantityB) +
" gallon from C to B. " + newState->to_string();
else
direction += "Pour " + to_string(quantityB) +
" gallons from C to B. " + newState->to_string();
}
newState->directions = direction;
// Check if newState is already in the matrix
if (matrix[newState->a][newState->b] != nullptr)
delete newState;
else
stateQueue.push(newState);
} // C into B ends
//debug
//cout << stateQueue.front()->a << "---" << stateQueue.front()->b << "----" << stateQueue.front()->c << endl;
//cout << "Goals:--" << goalA << "------" << goalB << "------" << goalC<< endl;
}
for (auto &rows : matrix)
for (auto &columns : rows)
delete columns; // deleting the array
delete initial;
return "No solution.";
}
int main(int argc, char *argv[]) {
string inputs_letters[6] = {"A", "B", "C", "A", "B", "C"};
string inputs_type[6] = {"capacity", "capacity", "capacity",
"goal", "goal", "goal"};
istringstream iss;
if (argc != 7) { // check for corrent amount of inputs
cerr << "Usage: " << argv[0]
<< " <cap A> <cap B> <cap C> <goal A> <goal B> <goal C>" << endl;
return 1;
}
vector<int> list(6);
for (int i = 1; i < 7; i++) {
iss.str(argv[i]);
if (!(iss >> list[i - 1]) || list[(i - 1) % 3] <= 0 ||
list[i - 1] < 0) { // check to see if each input is an int or capacity
// is > 0 or goal is >= 0
cerr << "Error: Invalid " << inputs_type[i - 1] << " '" << argv[i] << "' "
<< "for jug " << inputs_letters[i - 1] << "." << endl;
return 1;
}
iss.clear();
}
if (list[3] > list[0]) { // check if goal is greater than capacity
cerr << "Error: Goal cannot exceed capacity of jug " << inputs_letters[3]
<< "." << endl;
return 1;
}
if (list[4] > list[1]) {
cerr << "Error: Goal cannot exceed capacity of jug " << inputs_letters[4]
<< "." << endl;
return 1;
}
if (list[5] > list[2]) {
cerr << "Error: Goal cannot exceed capacity of jug " << inputs_letters[5]
<< "." << endl;
return 1;
}
if (list[3] + list[4] + list[5] !=
list[2]) { // check if total gallons in goal state is greater than the
// capactiy of C
cerr << "Error: Total gallons in goal state must be equal to the capacity "
"of jug C."
<< endl;
return 1;
}
if (bfs(list[0], list[1], list[2], list[3], list[4], list[5]) ==
"No solution.") {
cout << "No solution." << endl;
return 1;
}
cout << bfs(list[0], list[1], list[2], list[3], list[4], list[5]) << endl;
return 0;
}