-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwater.cpp
More file actions
298 lines (263 loc) · 9.27 KB
/
water.cpp
File metadata and controls
298 lines (263 loc) · 9.27 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
// File: $Id: jugs.cpp,v 1.1 2014/11/30 11:15:00 Exp $
// Author: Satyajeet Shahane
/*
* Problem statement :
A man goes to a river with several containers of different sizes to get a specific quantity of water in one of the containers.
He can perform a sequence of the following operations:
1.Fill any container from the river
2.Empty any container
3.Pour the contents of any container into any other container until either the destination container is full or the source container is empty.
What sequence of operations does he perform to obtain the desired quantity of water in one of his containers?
*/
/*
* Class Description :Individual water jug puzzle class which exhibits solution to puzzle with the aid of Generic framework functions.
* */
//
// Revisions:
// $Log: jugs.cpp,v $
// Revision 1.1 2014/11/30 11:15:00
#include <iostream>
#include <vector>
#include "problem.h"
#include "solver.cpp"
//#define NUMBER_OF_JUGS 2
using namespace std;
int NUMBER_OF_JUGS = 0; //GLOBAL!
class Node {
public:
vector<int> jugs;
vector<int> capacity;
Node *parent;
/*
* Default constructor
*/
Node() {
int i;
for ( i = 0 ; i < NUMBER_OF_JUGS ; i++ ) {
jugs.push_back(0);
capacity.push_back(0);
}
parent = NULL;
}
/*
* Overloaded contructor
*/
Node(vector<int> maxVals) {
int i;
// NUMBER_OF_JUGS = n;
for ( i = 0 ; i < NUMBER_OF_JUGS ; i++ ) {
jugs.push_back(0);
capacity.push_back(maxVals[i]);
}
parent = NULL;
}
/*
* copies all the node data
*/
void duplicateIt(Node *n) {
int i;
for ( i = 0 ; i < NUMBER_OF_JUGS ; i++ ) {
jugs[i] = n->jugs[i];
capacity[i] = n->capacity[i];
}
parent = n->parent;
}
/*
* Displays all max values
*/
void dispMax() {
int i;
cout << endl << "Max vals: " << endl;
for ( i = 0 ; i < NUMBER_OF_JUGS ; i++ ) {
cout << capacity[i] << '\t';
}
}
/*
* Displays the current config
*/
void disp() {
int i;
for ( i = 0 ; i < NUMBER_OF_JUGS ; i++ ) {
cout << jugs[i] << '\t';
}
//cout << "this: " << this << '\t' << "parent: " << parent;
cout << endl;
}
/*
* returns number of jugs involved
*/
int numberOfJugs() {
return jugs.size();
}
/*
* checks if the param node is equal to the
* one on which this method is invoked
*/
bool equals(Node *node) {
int i;
for ( i = 0 ; i < jugs.size() ; i++ ) {
if ( jugs[i] != node->jugs[i] ) {
return false;
}
}
return true;
}
/*
* Method to write a config object to file...
*/
void writeObjectToFile(ofstream& op) {
//TBD
}
};
class JugsProblem: public problem<Node, int> {
public:
Node start;
int goal;
/*
* Default constructor
*/
JugsProblem() {}
/*
* Overridden constructor
*/
JugsProblem(Node start, int goal): problem(start, goal) {
this->start = start;
this->goal = goal;
}
/*
* returns list of adjecent and non-visited configs
*/
vector<Node*> nextConfiguration(Node *node) {
vector<Node*> next;
Node *dupNode; //duplicate node to do manipulations on...
int i,j,k;
for ( i = 0 ; i < node->numberOfJugs() ; i++ ) { //for all jugs
int jugCurrent = node->jugs[i];
int jugCapacity = node->capacity[i];
dupNode = new Node();
dupNode->duplicateIt(node);
//if jug is completely empty, fill it
if ( jugCurrent == 0 ) {
dupNode->jugs[i] = dupNode->capacity[i];
//push to next vector
next.push_back(dupNode);
} else if ( jugCurrent == jugCapacity ) { //if jug is completely full..
//CASE 1: empty it
dupNode->jugs[i] = 0;
next.push_back(dupNode);
dupNode = new Node();
dupNode->duplicateIt(node);
//CASE 2: transfer to all other jugs
for ( j = 0 ; j < node->numberOfJugs() ; j++ ) {
dupNode = new Node();
dupNode->duplicateIt(node);
int tempJugCurrent = node->jugs[j];
int tempJugCapacity = node->capacity[j];
if ( i != j ) {//if not the same jug
if ( tempJugCurrent != tempJugCapacity ) {//temp jug is not full
int maxTransferQty = tempJugCapacity - tempJugCurrent;
if ( jugCurrent > maxTransferQty ) {//jug temp should not overflow
int transferQty = maxTransferQty; //this much is actually transfered
dupNode->jugs[j] = dupNode->capacity[j];
dupNode->jugs[i] = dupNode->jugs[i] - transferQty;
next.push_back(dupNode);
} else {
dupNode->jugs[j] = dupNode->jugs[j] + jugCurrent;
dupNode->jugs[i] = 0;
next.push_back(dupNode);
}
}
}
}
} else if ( (jugCurrent > 0) && (jugCurrent < jugCapacity) ) { //if jug is partially full.
//empty the jug
dupNode->jugs[i] = 0;
next.push_back(dupNode);
dupNode = new Node();
dupNode->duplicateIt(node); //re-init
//fill completely
dupNode->jugs[i] = dupNode->capacity[i];
next.push_back(dupNode);
dupNode = new Node();
dupNode->duplicateIt(node); //re-init
for ( j = 0 ; j < node->numberOfJugs() ; j++ ) {
dupNode = new Node();
dupNode->duplicateIt(node); //re-init
int tempJugCurrent = node->jugs[j];
int tempJugCapacity = node->capacity[j];
if ( i != j ) {//if not the same jug
if ( tempJugCurrent != tempJugCapacity ) {//temp jug is not full
int maxTransferQty = tempJugCapacity - tempJugCurrent;
//2 = 5 - 3
if ( jugCurrent > maxTransferQty ) {//jug temp should not overflow
int transferQty = maxTransferQty; //this much is actually transfered
dupNode->jugs[j] = dupNode->capacity[j];
dupNode->jugs[i] = dupNode->jugs[i] - transferQty;
next.push_back(dupNode);
} else {
dupNode->jugs[j] = dupNode->jugs[j] + jugCurrent;
dupNode->jugs[i] = 0;
next.push_back(dupNode);
}
}
}
}
}
}
return next;
}
/*
* checks if any jug has quantity = goal
*/
bool isGoal(Node *current) {
int i;
for ( i = 0 ; i < NUMBER_OF_JUGS ; i++ ) {
if ( current->jugs[i] == goal ) {
return true;
}
}
return false;
}
/*
* Method to display start and end states for a given problem
*/
void dispStartEndStates() {
cout << "Start config: " << endl;
start.disp();
cout << "End value: " << goal << endl;
}
};
/*
* Main method: takes input from command line,
* creates object of the jugs problem and solves
* this problem by using the framework
*/
int main(int argc, char *argv[]) {
if ( argc < 4 ) {
cout << "Incorrect input..." << endl;
return 0;
}
vector<int> maxVals;
NUMBER_OF_JUGS = argc - 2;
int i,j, goal;
//set goal quantity
goal = (char)*argv[1];
goal = goal - 48;
//number of jugs need to be less than 10
for ( j = 0 , i = 2 ; i < argc; i++, j++ ) {
//my apologies for this bad piece of code
int temp;
temp = (char)*argv[i];
temp = temp - 48;
maxVals.push_back(temp);
}
Node startState(maxVals);
JugsProblem jp(startState, goal);
ProblemSolver<Node, int> ps(jp, false);
if ( ps.solve(&jp) ) {
ps.displayPath();
} else {
cout << "No solution found..." << endl;
}
return 0;
}