// Autor: Mikołaj Janusz #include <bits/stdc++.h> typedef long long ll; using namespace std; ll max_a, max_b, max_c, start_a, start_b, start_c;; struct cords { ll a; ll b; ll c; cords(ll a_, ll b_, ll c_) { a = a_; b = b_; c = c_; } bool operator < (const cords& r) const { if (a != r.a) return a < r.a; else if (b != r.b) return b < r.b; return c < r.c; } bool operator > (const cords& r) const { if (a != r.a) return a > r.a; else if (b != r.b) return b > r.b; return c > r.c; } }; bool checkCoordinates(ll a, ll b, ll c){ if (0 <= a && a <= max_a) { if (0 <= b && b <= max_b) { if (0 <= c && c <= max_c) { if (a + b + c == start_a + start_b + start_c) { return true; } } } } return false; } bool check_and_push(ll a, ll b, ll c, set<cords>& visited, queue<cords>& q) { if (checkCoordinates(a, b, c)) { if (visited.find(cords(a, b, c)) == visited.end()) { cords temp = cords(a, b, c); q.push(temp); visited.insert(temp); return true; } } return false; } void update_mins(ll a, ll b, ll c, ll iteration, vector<ll>& targets) { targets[a] = min(targets[a], iteration); targets[b] = min(targets[b], iteration); targets[c] = min(targets[c], iteration); } int main() { ios::sync_with_stdio(false); cin >> max_a >> max_b >> max_c >> start_a >> start_b >> start_c; vector<ll> targets(max(max_a, max(max_b, max_c)) + 1, 999999999); set<cords> visited; queue<cords> q; q.push(cords(start_a, start_b, start_c)); visited.insert(cords(start_a, start_b, start_c)); update_mins(start_a, start_b, start_c, 0, targets); ll tmpa, tmpb, tmpc, counter; for (ll iteration = 0; q.size() != 0; iteration++) { for (ll i = q.size(); i > 0; i--){ cords current = q.front(); q.pop(); tmpa = current.a + min(max_a - current.a, current.b); tmpb = current.b - min(max_a - current.a, current.b); tmpc = current.c; if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } tmpa = current.a - min(max_b - current.b, current.a); tmpb = current.b + min(max_b - current.b, current.a); tmpc = current.c; if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } tmpa = current.a + min(max_a - current.a, current.c); tmpb = current.b; tmpc = current.c - min(max_a - current.a, current.c); if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } tmpa = current.a - min(max_c - current.c, current.a); tmpb = current.b; tmpc = current.c + min(max_c - current.c, current.a); if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } tmpa = current.a; tmpb = current.b + min(max_b - current.b, current.c); tmpc = current.c - min(max_b - current.b, current.c); if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } tmpa = current.a; tmpb = current.b - min(max_c - current.c, current.b); tmpc = current.c + min(max_c - current.c, current.b); if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } } } for (ll i = 0; i < (max(max_a, max(max_b, max_c)) + 1); i++) { ll res; res = targets[i] < 999999999LL ? targets[i] : -1; cout << res << " "; } cout << endl; return 0; }
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | // Autor: Mikołaj Janusz #include <bits/stdc++.h> typedef long long ll; using namespace std; ll max_a, max_b, max_c, start_a, start_b, start_c;; struct cords { ll a; ll b; ll c; cords(ll a_, ll b_, ll c_) { a = a_; b = b_; c = c_; } bool operator < (const cords& r) const { if (a != r.a) return a < r.a; else if (b != r.b) return b < r.b; return c < r.c; } bool operator > (const cords& r) const { if (a != r.a) return a > r.a; else if (b != r.b) return b > r.b; return c > r.c; } }; bool checkCoordinates(ll a, ll b, ll c){ if (0 <= a && a <= max_a) { if (0 <= b && b <= max_b) { if (0 <= c && c <= max_c) { if (a + b + c == start_a + start_b + start_c) { return true; } } } } return false; } bool check_and_push(ll a, ll b, ll c, set<cords>& visited, queue<cords>& q) { if (checkCoordinates(a, b, c)) { if (visited.find(cords(a, b, c)) == visited.end()) { cords temp = cords(a, b, c); q.push(temp); visited.insert(temp); return true; } } return false; } void update_mins(ll a, ll b, ll c, ll iteration, vector<ll>& targets) { targets[a] = min(targets[a], iteration); targets[b] = min(targets[b], iteration); targets[c] = min(targets[c], iteration); } int main() { ios::sync_with_stdio(false); cin >> max_a >> max_b >> max_c >> start_a >> start_b >> start_c; vector<ll> targets(max(max_a, max(max_b, max_c)) + 1, 999999999); set<cords> visited; queue<cords> q; q.push(cords(start_a, start_b, start_c)); visited.insert(cords(start_a, start_b, start_c)); update_mins(start_a, start_b, start_c, 0, targets); ll tmpa, tmpb, tmpc, counter; for (ll iteration = 0; q.size() != 0; iteration++) { for (ll i = q.size(); i > 0; i--){ cords current = q.front(); q.pop(); tmpa = current.a + min(max_a - current.a, current.b); tmpb = current.b - min(max_a - current.a, current.b); tmpc = current.c; if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } tmpa = current.a - min(max_b - current.b, current.a); tmpb = current.b + min(max_b - current.b, current.a); tmpc = current.c; if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } tmpa = current.a + min(max_a - current.a, current.c); tmpb = current.b; tmpc = current.c - min(max_a - current.a, current.c); if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } tmpa = current.a - min(max_c - current.c, current.a); tmpb = current.b; tmpc = current.c + min(max_c - current.c, current.a); if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } tmpa = current.a; tmpb = current.b + min(max_b - current.b, current.c); tmpc = current.c - min(max_b - current.b, current.c); if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } tmpa = current.a; tmpb = current.b - min(max_c - current.c, current.b); tmpc = current.c + min(max_c - current.c, current.b); if (check_and_push(tmpa, tmpb, tmpc, visited, q)) { update_mins(tmpa, tmpb, tmpc, iteration + 1, targets); } } } for (ll i = 0; i < (max(max_a, max(max_b, max_c)) + 1); i++) { ll res; res = targets[i] < 999999999LL ? targets[i] : -1; cout << res << " "; } cout << endl; return 0; } |