-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
33 lines (33 loc) · 778 Bytes
/
s1.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
class Solution {
private:
set<int> s;
queue<int> q;
void push(int v) {
if (!s.count(v) && v) {
s.insert(v);
q.push(v);
}
}
public:
bool canMeasureWater(int x, int y, int z) {
if (!z || x == z || y == z) return true;
if (x == y) return x == z || 2 * x == z;
if (x < y) swap(x, y);
q.push(x);
q.push(y);
while (q.size()) {
int v = q.front();
if (v == z) return true;
q.pop();
if (v <= x) {
if (v > y) push(v - y);
else {
push(v + x);
push(x - y + v);
}
push(v + y);
}
}
return false;
}
};