比赛 2025暑期集训第7场 评测结果 MWMMMAMMMM
题目名称 倒水 最终得分 10
用户昵称 对立猫猫对立 运行时间 1.966 s
代码语言 C++ 内存使用 105.81 MiB
提交时间 2025-08-11 17:27:16
显示代码纯文本
#include <unordered_map>
#include <tuple>
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
int a, b, c, x;

//C++11 Hash-tuple
template <typename T>
inline void hash_combine(size_t& seed, const T& val) {
    seed ^= hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
struct tuple_hash {
    template <typename... TT>
    size_t operator()(const tuple<TT...>& tt) const {
        size_t seed = 0;
        hash_combine(seed, get<0>(tt));
        hash_combine(seed, get<1>(tt));
        hash_combine(seed, get<2>(tt));
        return seed;
    }
};


unordered_map<tuple<int, int, int>, int, tuple_hash> dp;
int dfs(int nowa, int nowb, int nowc, int target) {
    if (nowa == target || nowb == target || nowc == target) {
        return 0;
    }
    auto current_state = make_tuple(nowa, nowb, nowc);
    if (dp.find(current_state) != dp.end()) {
        return dp[current_state];
    }
    int ans = 0x3f3f3f3f;
    int alast = a - nowa;
    int blast = b - nowb;
    int clast = c - nowc;
    if (nowa != 0) {
        if (nowa > blast) {
            int temp = dfs(nowa - blast, b, nowc, target);
            ans = min(ans, temp + 1);
        } else {
            int temp = dfs(0, nowb + nowa, nowc, target);
            ans = min(ans, temp + 1);
        }
        if (nowa > clast) {
            int temp = dfs(nowa - clast, nowb, c, target);
            ans = min(ans, temp + 1);
        } else {
            int temp = dfs(0, nowb, nowc + nowa, target);
            ans = min(ans, temp + 1);
        }
    }
    if (nowb != 0) {
        if (nowb > alast) {
            int temp = dfs(a, nowb - alast, nowc, target);
            ans = min(ans, temp + 1);
        } else {
            int temp = dfs(nowa + nowb, 0, nowc, target);
            ans = min(ans, temp + 1);
        }
        if (nowb > clast) {
            int temp = dfs(nowa, nowb - clast, c, target);
            ans = min(ans, temp + 1);
        } else {
            int temp = dfs(nowa, 0, nowc + nowb, target);
            ans = min(ans, temp + 1);
        }
    }
    if (nowc != 0) {
        if (nowc > alast) {
            int temp = dfs(a, nowb, nowc - alast, target);
            ans = min(ans, temp + 1);
        } else {
            int temp = dfs(nowa + nowc, nowb, 0, target);
            ans = min(ans, temp + 1);
        }
        if (nowc > blast) {
            int temp = dfs(nowa, b, nowc - blast, target);
            ans = min(ans, temp + 1);
        } else {
            int temp = dfs(nowa, nowb + nowc, 0, target);
            ans = min(ans, temp + 1);
        }
    }
    dp[current_state] = ans;
    return ans;
}
int main() {
	freopen("pourwater.in", "r", stdin);
	freopen("pourwater.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> a >> b >> c >> x;
    if (a == x || b == x || c == x) {
        cout << 0 << endl;
        return 0;
    }
    int result = dfs(a, b, c, x);
    if (result == 0x3f3f3f3f) {
        cout << "false" << endl;
    } else {
        cout << result << endl;
    }
    return 0;
}