-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBottles.cpp
205 lines (176 loc) · 5.08 KB
/
Bottles.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
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
#include "Bottles.h"
#include <iostream>
using namespace std;
Bottles::Bottles()
{
actionName = "";
prev = nullptr;
}
Bottles::Bottles(string b[], int numberOfBottles)
{
for (int i=0; i<numberOfBottles; i++)
bottles.push_back(b[i]);
actionName = "";
prev = nullptr;
}
string Bottles::toString () const
{
stringstream ot;
for(const auto& bottle : bottles){
if(bottle.size()==MAX_BOTTLE_CAPACITY){ //if the bottle is full
ot<<bottle<<';';
}
else if(!bottle.empty() && bottle.size()<MAX_BOTTLE_CAPACITY){ //if the bottle is not full but also not empty
ot<<bottle;
for(int i=0; i<MAX_BOTTLE_CAPACITY-bottle.size(); i++){
ot<<"_";
}
ot<<";";
}
else{ //if the bottle is empty
ot<<"____;";
}
}
return ot.str();
}
bool Bottles::isSolved()
{
for (auto bottle : bottles) {
if (bottle.empty()) //if the bottle is empty
continue;
else if (bottle.size() < MAX_BOTTLE_CAPACITY) //if the bottle is not full
return false;
else if (count(bottle.begin(),
bottle.end(),
bottle[0])
!= MAX_BOTTLE_CAPACITY) //if the bottle is not same colored
return false;
}
return true;
}
int Bottles::isValidMove(string sourceBottle, string destinationBottle)
{
//Can't pour from an empty bottle or to a full bottle
if (sourceBottle.empty() || destinationBottle.size() == MAX_BOTTLE_CAPACITY)
return 0;
int colorFreqs = count(sourceBottle.begin(),
sourceBottle.end(),
sourceBottle[0]);
//If the source bottle is same colored and full, don't touch it
if (colorFreqs == MAX_BOTTLE_CAPACITY)
return 0;
int k=1;
char topColor = sourceBottle.back(); //source bottle is not empty
int i=0;
while(i < sourceBottle.size()-1){
if(sourceBottle[sourceBottle.size() - (i+2)] == topColor){
k++;
}
else{
break;
}
i++;
}
//k ml of the same colored liquid at the top of the source bottle
if (destinationBottle.empty()) {
//If source bottle has only same colored liquid, don't touch it (to avoid loops)
if (colorFreqs == sourceBottle.size())
return 0;
return k; //return k ml
}
//In any other case check the top colors of the two bottles
//and if the k ml fit in the destination bottle
if(sourceBottle[sourceBottle.size() - 1] == destinationBottle[destinationBottle.size() - 1]){
if(k <= MAX_BOTTLE_CAPACITY - destinationBottle.size()){ //if k ml fit in the destination bottle
return k;
}
else{
return MAX_BOTTLE_CAPACITY - destinationBottle.size(); //ml that fit in the destination bottle
}
}
else{
return 0;
}
}
bool Bottles::makeMove(Bottles &n, string sourceBottle, string destinationBottle, int i, int j)
{
int k = isValidMove(sourceBottle, destinationBottle);
if(k>0){
n=*this;
for (int l=0; l < k; l++){
n.bottles[j].push_back(n.bottles[i].back());
n.bottles[i].pop_back();
}
string str_i = to_string(i+1);
string str_j = to_string(j+1);
//adding space to 1-digit numbers to align the output
if(i<9)
str_i=" "+str_i;
if(j<9)
str_j=" "+str_j;
n.setActionName("Pour "+to_string(k)+" ml from "+str_i+" to "+str_j+";");
n.setPrevious(this);
return true;
}
return false;
}
vector <Bottles *> Bottles::expand()
{
vector <Bottles *> children;
Bottles *child;
for (int i = 0; i < bottles.size(); i++) {
//Iterate over all the bottles
string sourceBottle = bottles[i];
for (int j = 0; j < bottles.size(); j++) {
if (i == j)
continue;
string destinationBottle = bottles[j];
child = new Bottles(*this);
if(makeMove(*child,sourceBottle,destinationBottle,i,j)) {
children.push_back(child);
}
else {
delete child;
}
}
}
return children;
}
int Bottles::getDepth()
{
int counter =0;
Bottles *p = this;
while (p->prev!=nullptr)
{
p=p->prev;
counter++;
}
return counter;
}
ostream & operator<<(ostream &stream, const Bottles &a)
{
string s; //s="";
s=a.getActionName();
Bottles *p=a.getPrevious();
while(p!=nullptr)
{
s = p->getActionName()+"\n"+s;
p = p->getPrevious();
}
stream<<s;
return stream;
}
void Bottles::printStates() const{
string s; //s="";
s=getActionName()+" "+toString();
Bottles *p=getPrevious();
while(p!=nullptr)
{
if(p->getPrevious()== nullptr)
s="Starting from: "+p->toString()+"\n"+s;
else
s = p->getActionName()+" "+p->toString()+"\n"+s;
p = p->getPrevious();
}
cout<<s;
}