From 6839d2ea13b8ef7ecd7a878baf5e1dc0a9f69de1 Mon Sep 17 00:00:00 2001 From: e2hang <2099307493@qq.com> Date: Tue, 2 Sep 2025 08:59:59 +0800 Subject: [PATCH] Altered --- .../DS-Related/Graph/DFS/UVa10603-Fill.cpp | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Algorithm/DS-Related/Graph/DFS/UVa10603-Fill.cpp diff --git a/Algorithm/DS-Related/Graph/DFS/UVa10603-Fill.cpp b/Algorithm/DS-Related/Graph/DFS/UVa10603-Fill.cpp new file mode 100644 index 0000000..57c46e3 --- /dev/null +++ b/Algorithm/DS-Related/Graph/DFS/UVa10603-Fill.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +using namespace std; + +bool have = false; +set> visited; + +int A, B, C; // 三个容器的容量 + +void dfs(int a, int b, int c, int target) { + if (a == target || b == target || c == target) { + have = true; + return; + } + + auto state = make_tuple(a, b, c); + if (visited.find(state) != visited.end()) return; + visited.insert(state); + + // 六种倒水操作 + // a -> b + { + int pour = min(a, B - b); + dfs(a - pour, b + pour, c, target); + } + // a -> c + { + int pour = min(a, C - c); + dfs(a - pour, b, c + pour, target); + } + // b -> a + { + int pour = min(b, A - a); + dfs(a + pour, b - pour, c, target); + } + // b -> c + { + int pour = min(b, C - c); + dfs(a, b - pour, c + pour, target); + } + // c -> a + { + int pour = min(c, A - a); + dfs(a + pour, b, c - pour, target); + } + // c -> b + { + int pour = min(c, B - b); + dfs(a, b + pour, c - pour, target); + } +} + +int main() { + int target; + cin >> A >> B >> C >> target; + dfs(A, 0, 0, target); // 假设初始是 A 装满 + if (have) cout << "YES" << endl; + else cout << "NO" << endl; + return 0; +} +