|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+import java.util.ArrayList;
|
|
|
2
|
+import java.util.Objects;
|
|
|
3
|
+
|
|
|
4
|
+class TwoBucket {
|
|
|
5
|
+
|
|
|
6
|
+ private class State {
|
|
|
7
|
+ int moves;
|
|
|
8
|
+ int bucketOne;
|
|
|
9
|
+ int bucketTwo;
|
|
|
10
|
+
|
|
|
11
|
+ State (int moves, int bucketOne, int bucketTwo) {
|
|
|
12
|
+ this.moves = moves;
|
|
|
13
|
+ this.bucketOne = bucketOne;
|
|
|
14
|
+ this.bucketTwo = bucketTwo;
|
|
|
15
|
+ }
|
|
|
16
|
+
|
|
|
17
|
+ @Override
|
|
|
18
|
+ public boolean equals(Object o) {
|
|
|
19
|
+ State otherState = (State) o;
|
|
|
20
|
+ return this.moves == otherState.moves &&
|
|
|
21
|
+ this.bucketOne == otherState.bucketOne &&
|
|
|
22
|
+ this.bucketTwo == otherState.bucketTwo;
|
|
|
23
|
+ }
|
|
|
24
|
+
|
|
|
25
|
+ @Override
|
|
|
26
|
+ public int hashCode() {
|
|
|
27
|
+ return Objects.hash(moves, bucketOne, bucketTwo);
|
|
|
28
|
+ }
|
|
|
29
|
+ }
|
|
|
30
|
+
|
|
|
31
|
+ private State finalState;
|
|
|
32
|
+
|
|
|
33
|
+ private int bucketOneCap;
|
|
|
34
|
+ private int bucketTwoCap;
|
|
|
35
|
+ private int desiredLiters;
|
|
|
36
|
+ private String startBucket;
|
|
|
37
|
+
|
|
|
38
|
+ TwoBucket(int bucketOneCap, int bucketTwoCap, int desiredLiters, String startBucket) {
|
|
|
39
|
+ this.bucketOneCap = bucketOneCap;
|
|
|
40
|
+ this.bucketTwoCap = bucketTwoCap;
|
|
|
41
|
+ this.desiredLiters = desiredLiters;
|
|
|
42
|
+ this.startBucket = startBucket;
|
|
|
43
|
+
|
|
|
44
|
+ finalState = computeFinalState();
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
|
47
|
+ private ArrayList<State> getAdjacentStates (State state) {
|
|
|
48
|
+ ArrayList<State> adjacentStates = new ArrayList<State>();
|
|
|
49
|
+
|
|
|
50
|
+ //Empty bucket one
|
|
|
51
|
+ adjacentStates.add(new State(state.moves + 1, 0, state.bucketTwo));
|
|
|
52
|
+
|
|
|
53
|
+ //Empty bucket two
|
|
|
54
|
+ adjacentStates.add(new State(state.moves + 1, state.bucketOne, 0));
|
|
|
55
|
+
|
|
|
56
|
+ //Fill bucket one
|
|
|
57
|
+ adjacentStates.add(new State(state.moves + 1, bucketOneCap, state.bucketTwo));
|
|
|
58
|
+
|
|
|
59
|
+ //Fill bucket two
|
|
|
60
|
+ adjacentStates.add(new State(state.moves + 1, state.bucketOne, bucketTwoCap));
|
|
|
61
|
+
|
|
|
62
|
+ //pour from bucket one to bucket two
|
|
|
63
|
+ if (state.bucketOne + state.bucketTwo > bucketTwoCap) {
|
|
|
64
|
+ adjacentStates.add(new State(state.moves + 1, state.bucketOne - (bucketTwoCap - state.bucketTwo), bucketTwoCap));
|
|
|
65
|
+ } else {
|
|
|
66
|
+ adjacentStates.add(new State(state.moves + 1, 0, state.bucketOne + state.bucketTwo));
|
|
|
67
|
+ }
|
|
|
68
|
+
|
|
|
69
|
+ //pour from bucket two to bucket one
|
|
|
70
|
+ if (state.bucketTwo + state.bucketOne > bucketOneCap) {
|
|
|
71
|
+ adjacentStates.add(new State(state.moves + 1, bucketOneCap, state.bucketTwo - (bucketOneCap - state.bucketOne)));
|
|
|
72
|
+ } else {
|
|
|
73
|
+ adjacentStates.add(new State(state.moves + 1, state.bucketTwo + state.bucketOne, 0));
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ return adjacentStates;
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ private boolean isValid(State state) {
|
|
|
80
|
+ if (state.bucketOne == bucketOneCap && state.bucketTwo == 0 && startBucket.equals("two")) {
|
|
|
81
|
+ return false;
|
|
|
82
|
+ } else if (state.bucketOne == 0 && state.bucketTwo == bucketTwoCap && startBucket.equals("two")) {
|
|
|
83
|
+ return false;
|
|
|
84
|
+ } else {
|
|
|
85
|
+ return true;
|
|
|
86
|
+ }
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ private State computeFinalState() {
|
|
|
90
|
+ ArrayList<State> paths = new ArrayList<State>();
|
|
|
91
|
+
|
|
|
92
|
+ State initialState;
|
|
|
93
|
+ if (startBucket.equals("one")) {
|
|
|
94
|
+ initialState = new State(1, bucketOneCap, 0);
|
|
|
95
|
+ } else {
|
|
|
96
|
+ initialState = new State(1, 0, bucketTwoCap);
|
|
|
97
|
+ }
|
|
|
98
|
+
|
|
|
99
|
+ if (initialState.bucketOne == desiredLiters || initialState.bucketTwo == desiredLiters) {
|
|
|
100
|
+ return initialState;
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ paths.add(initialState);
|
|
|
104
|
+
|
|
|
105
|
+ for (int i = 0; i < 10000; i++) {
|
|
|
106
|
+ State currentState = paths.remove(0);
|
|
|
107
|
+ ArrayList<State> adjacentStates = getAdjacentStates(currentState);
|
|
|
108
|
+ for (State state : adjacentStates) {
|
|
|
109
|
+ if (state.bucketOne == desiredLiters || state.bucketTwo == desiredLiters) {
|
|
|
110
|
+ return state;
|
|
|
111
|
+ }
|
|
|
112
|
+
|
|
|
113
|
+ if (!paths.contains(state) && isValid(state)) {
|
|
|
114
|
+ paths.add(state);
|
|
|
115
|
+ }
|
|
|
116
|
+ }
|
|
|
117
|
+ }
|
|
|
118
|
+
|
|
|
119
|
+ return null;
|
|
|
120
|
+ }
|
|
|
121
|
+
|
|
|
122
|
+ int getTotalMoves() {
|
|
|
123
|
+ return finalState.moves;
|
|
|
124
|
+ }
|
|
|
125
|
+
|
|
|
126
|
+ String getFinalBucket() {
|
|
|
127
|
+ if (finalState.bucketOne == desiredLiters) {
|
|
|
128
|
+ return "one";
|
|
|
129
|
+ } else if(finalState.bucketTwo == desiredLiters) {
|
|
|
130
|
+ return "two";
|
|
|
131
|
+ } else {
|
|
|
132
|
+ return "No solution found in " + finalState.moves + " iterations!";
|
|
|
133
|
+ }
|
|
|
134
|
+ }
|
|
|
135
|
+
|
|
|
136
|
+ int getOtherBucket() {
|
|
|
137
|
+ if (getFinalBucket().equals("one")) {
|
|
|
138
|
+ return finalState.bucketTwo;
|
|
|
139
|
+ } else if(getFinalBucket().equals("two")) {
|
|
|
140
|
+ return finalState.bucketOne;
|
|
|
141
|
+ } else {
|
|
|
142
|
+ return -1;
|
|
|
143
|
+ }
|
|
|
144
|
+ }
|
|
|
145
|
+}
|