-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPourWater.cpp
More file actions
70 lines (55 loc) · 2.11 KB
/
PourWater.cpp
File metadata and controls
70 lines (55 loc) · 2.11 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
// g++ --std=c++17 donuoc.cc -o donuoc
#include<iostream>
#include<tuple>
#include <map>
#include <vector>
using namespace std;
typedef tuple<int, int, int> Vertex;
// (10 lit, 7 lit, 4 lit)
map<Vertex, bool> visited;
void printState (Vertex &u, Vertex &v){
int l10,l7,l4;
tie(l10,l7,l4) = u;
cout<<"\"("<<l10<<","<<l7<<","<<l4<<")\" "<<"->";
tie(l10,l7,l4) = v;
cout<<"\"("<<l10<<","<<l7<<","<<l4<<")\";"<<endl;
}
void explore (Vertex &s) {
visited[s] = true;
int l10, l7, l4;
tie(l10, l7, l4) = s;
if (l7==2 or l4==2) exit(0);
int do_max;
// Place state consecutively to pouring water from each tank to the other tank by for loop
vector<int> state = {l10, l7, l4, l10, l7}; // The state which we consider to action
vector<int> max = {10, 7, 4, 10, 7}; // Max volume of each tank
map<int, int> result; // Result of tank
for (int i = 0; i < 3; i++) {
if (state[i] > 0) { // if volume in tank > 0 which mean we can pour out
for(int j = 1; j < 3; j++) {
if (state[i + j] < max[i + j]) { // if volume in tank < max of it which mean we can pour in
do_max = min ((max[i + j] - state[i + j]), state[i]);
result[max[i]] = state[i] - do_max;
result[max[i + j]] = state[i+j] + do_max;
if (j == 1) {
result[max[i + 2]] = state[i+2];
} else {
result[max[i + 1]] = state[i+1];
}
Vertex v = make_tuple(result[10], result[7], result[4]);
if (!visited[v]) {
printState (s, v);
explore(v);
}
}
}
}
}
}
int main() {
// Max of first is 10, second is 7, third is 4
// We start at first tank is 0, second is 7, third is 4
Vertex start = make_tuple(0, 7, 4);
explore(start);
return 0;
}