#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; typedef std::array<int, 3> Trujka; Trujka lim; std::vector<Trujka> somsiads(Trujka t){ std::vector<Trujka> ret; FOR(i, 3){ FOR(j, 3){ if(i == j) continue; int ile = std::min(lim[i] - t[i], t[j]); auto cur = t; cur[j] -= ile; cur[i] += ile; ret.push_back(cur); } } return ret; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); FOR(i, 3) std::cin >> lim[i]; Trujka start; FOR(i, 3) std::cin >> start[i]; std::queue<Trujka> que; que.push(start); std::map<Trujka, int> dist; dist[start] = 0; constexpr int INF = 1000000666; VI ans(lim[2]+1, INF); FOR(j, 3) ans[start[j]] = 0; while(SZ(que)){ auto v = que.front(); que.pop(); TRAV(ch, somsiads(v)){ if(dist.count(ch) == 0){ dist[ch] = dist[v] + 1; que.push(ch); FOR(j, 3) ans[ch[j]] = std::min(ans[ch[j]], dist[ch]); } } } TRAV(i, ans) std::cout << (i == INF ? -1 : i) << " "; std::cout << "\n"; 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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; typedef std::array<int, 3> Trujka; Trujka lim; std::vector<Trujka> somsiads(Trujka t){ std::vector<Trujka> ret; FOR(i, 3){ FOR(j, 3){ if(i == j) continue; int ile = std::min(lim[i] - t[i], t[j]); auto cur = t; cur[j] -= ile; cur[i] += ile; ret.push_back(cur); } } return ret; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); FOR(i, 3) std::cin >> lim[i]; Trujka start; FOR(i, 3) std::cin >> start[i]; std::queue<Trujka> que; que.push(start); std::map<Trujka, int> dist; dist[start] = 0; constexpr int INF = 1000000666; VI ans(lim[2]+1, INF); FOR(j, 3) ans[start[j]] = 0; while(SZ(que)){ auto v = que.front(); que.pop(); TRAV(ch, somsiads(v)){ if(dist.count(ch) == 0){ dist[ch] = dist[v] + 1; que.push(ch); FOR(j, 3) ans[ch[j]] = std::min(ans[ch[j]], dist[ch]); } } } TRAV(i, ans) std::cout << (i == INF ? -1 : i) << " "; std::cout << "\n"; return 0; } |