10942022-03-02 20:56:23peti1234Vizeskannákcpp14Accepted 40/4014ms3332 KiB
#include <bits/stdc++.h>

using namespace std;
int m1, m2, m3, l;
set<pair<int, int> > volt;
queue<pair<pair<int, int>, int> > q;
void pb(int a, int b, int db) {
    if (volt.find({a, b})==volt.end()) {
        volt.insert({a, b});
        q.push({{a, b}, db});
    }
}
int main()
{
    cin >> m1 >> m2 >> m3 >> l;
    pb(m1, 0, 0);
    while (q.size()>0) {
        int a=q.front().first.first, b=q.front().first.second, c=m1-a-b, db=q.front().second;
        q.pop();
        if (a==l || b==l || c==l) {
            cout << db << "\n";
            return 0;
        }
        db++;

        int kul=min(a, m2-b);
        pb(a-kul, b+kul, db);

        kul=min(m1-a, b);
        pb(a+kul, b-kul, db);

        kul=min(a, m3-c);
        pb(a-kul, b, db);

        kul=min(m1-a, c);
        pb(a+kul, b, db);

        kul=min(b, m3-c);
        pb(a, b-kul, db);

        kul=min(m2-b, c);
        pb(a, b+kul, db);

    }
    cout << -1 << "\n";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/02ms1776 KiB
2Accepted0/01ms1784 KiB
3Accepted1/11ms1844 KiB
4Accepted1/11ms1852 KiB
5Accepted1/11ms1860 KiB
6Accepted1/11ms1856 KiB
7Accepted1/11ms1864 KiB
8Accepted1/11ms1864 KiB
9Accepted1/11ms1864 KiB
10Accepted1/11ms1876 KiB
11Accepted1/11ms1876 KiB
12Accepted1/11ms1884 KiB
13Accepted2/22ms1892 KiB
14Accepted2/21ms1892 KiB
15Accepted2/21ms1892 KiB
16Accepted2/21ms1892 KiB
17Accepted2/21ms1908 KiB
18Accepted2/21ms1904 KiB
19Accepted2/21ms1908 KiB
20Accepted2/22ms1956 KiB
21Accepted2/21ms1920 KiB
22Accepted2/21ms1920 KiB
23Accepted2/21ms1940 KiB
24Accepted2/27ms2776 KiB
25Accepted2/24ms2484 KiB
26Accepted2/214ms3332 KiB
27Accepted2/28ms3060 KiB