比赛 2025暑期集训第7场 评测结果 AAAAAAAAAA
题目名称 倒水 最终得分 100
用户昵称 wdsjl 运行时间 0.029 s
代码语言 C++ 内存使用 3.66 MiB
提交时间 2025-08-11 15:52:15
显示代码纯文本
#include <iostream>
#include <queue>
#include <set>
using namespace std;

struct State {
    int a, b, c;
    int steps;
    
    State(int a_, int b_, int c_, int steps_) 
        : a(a_), b(b_), c(c_), steps(steps_) {}
};

struct StateCompare {
    bool operator()(const State& s1, const State& s2) const {
        if (s1.a != s2.a) return s1.a < s2.a;
        if (s1.b != s2.b) return s1.b < s2.b;
        return s1.c < s2.c;
    }
};

bool isTarget(int a, int b, int c, int x) {
    return a == x || b == x || c == x;
}

int solve(int A, int B, int C, int x) {
    int a = A, b = 0, c = 0;
    
    if (isTarget(a, b, c, x)) return 0;
    
    queue<State> q;
    set<State, StateCompare> visited;
    
    q.push(State(a, b, c, 0));
    visited.insert(State(a, b, c, 0));
    
    while (!q.empty()) {
        State curr = q.front();
        q.pop();
        
        if (curr.a > 0 && curr.b < B) {
            int pour = min(curr.a, B - curr.b);
            int newA = curr.a - pour;
            int newB = curr.b + pour;
            int newC = curr.c;
            
            if (isTarget(newA, newB, newC, x)) {
                return curr.steps + 1;
            }
            
            State newState(newA, newB, newC, 0);
            if (visited.find(newState) == visited.end()) {
                visited.insert(newState);
                q.push(State(newA, newB, newC, curr.steps + 1));
            }
        }
        
        if (curr.a > 0 && curr.c < C) {
            int pour = min(curr.a, C - curr.c);
            int newA = curr.a - pour;
            int newB = curr.b;
            int newC = curr.c + pour;
            
            if (isTarget(newA, newB, newC, x)) {
                return curr.steps + 1;
            }
            
            State newState(newA, newB, newC, 0);
            if (visited.find(newState) == visited.end()) {
                visited.insert(newState);
                q.push(State(newA, newB, newC, curr.steps + 1));
            }
        }
        
        if (curr.b > 0 && curr.a < A) {
            int pour = min(curr.b, A - curr.a);
            int newA = curr.a + pour;
            int newB = curr.b - pour;
            int newC = curr.c;
            
            if (isTarget(newA, newB, newC, x)) {
                return curr.steps + 1;
            }
            
            State newState(newA, newB, newC, 0);
            if (visited.find(newState) == visited.end()) {
                visited.insert(newState);
                q.push(State(newA, newB, newC, curr.steps + 1));
            }
        }
        
        if (curr.b > 0 && curr.c < C) {
            int pour = min(curr.b, C - curr.c);
            int newA = curr.a;
            int newB = curr.b - pour;
            int newC = curr.c + pour;
            
            if (isTarget(newA, newB, newC, x)) {
                return curr.steps + 1;
            }
            
            State newState(newA, newB, newC, 0);
            if (visited.find(newState) == visited.end()) {
                visited.insert(newState);
                q.push(State(newA, newB, newC, curr.steps + 1));
            }
        }
        
        if (curr.c > 0 && curr.a < A) {
            int pour = min(curr.c, A - curr.a);
            int newA = curr.a + pour;
            int newB = curr.b;
            int newC = curr.c - pour;
            
            if (isTarget(newA, newB, newC, x)) {
                return curr.steps + 1;
            }
            
            State newState(newA, newB, newC, 0);
            if (visited.find(newState) == visited.end()) {
                visited.insert(newState);
                q.push(State(newA, newB, newC, curr.steps + 1));
            }
        }
        
        if (curr.c > 0 && curr.b < B) {
            int pour = min(curr.c, B - curr.b);
            int newA = curr.a;
            int newB = curr.b + pour;
            int newC = curr.c - pour;
            
            if (isTarget(newA, newB, newC, x)) {
                return curr.steps + 1;
            }
            
            State newState(newA, newB, newC, 0);
            if (visited.find(newState) == visited.end()) {
                visited.insert(newState);
                q.push(State(newA, newB, newC, curr.steps + 1));
            }
        }
    }
    
    return -1;
}

int main() {
	freopen("pourwater.in","r",stdin);
	freopen("pourwater.out","w",stdout); 
    int A, B, C, x;
    cin >> A >> B >> C >> x;
    
    if (x == 0) {
        cout << 0 << endl;
        return 0;
    }
    if (x > A && x > B && x > C) {
        cout << "false" << endl;
        return 0;
    }
    
    int result = solve(A, B, C, x);
    if (result == -1) {
        cout << "false" << endl;
    } else {
        cout << result << endl;
    }
    
    return 0;
}