#include <cstdio> #include <cstring> #include <deque> #include <array> #include <set> using namespace std; #define NMAX 100000 int results[NMAX+10]; int main() { int A[3], a[3]; int *b = a + 1, *c = a+2, *B = A+1, *C = A+2; int sum; scanf("%d%d%d%d%d%d", A, B, C, a, b, c); sum = *a + *b + *c; memset(&results, 0xff, sizeof(int)*(*C+1)); deque<array<int, 3>> queue; queue.push_back({*a, *b, 0}); array<int, 3> cur, nw; set<pair<int, int>> visited; int t; visited.emplace(*a, *b); while(!queue.empty()) { cur = queue.front(); queue.pop_front(); t = cur[2]; cur[2] = sum - cur[1] - cur[0]; for (int k = 0; k<3; ++k) { if (results[cur[k]] == -1) { results[cur[k]] = t; } } //printf("%d %d %d, %d\n", cur[0], cur[1], cur[2], t); for (int src = 0; src < 3; ++src) { for (int j = 1; j <= 2; ++j) { int dst = (src + j)%3; int unt = (src + (j^3))%3; if (cur[src] > 0 && cur[dst] < A[dst]) { nw[unt] = cur[unt]; int tmpsum = cur[src] + cur[dst]; if (tmpsum <= A[dst]) { nw[src] = 0; nw[dst] = tmpsum; } else { nw[dst] = A[dst]; nw[src] = tmpsum - A[dst]; } if (visited.count(make_pair(nw[0], nw[1])) == 0) { visited.emplace(nw[0], nw[1]); queue.push_back({nw[0], nw[1], t+1}); } } } } } for (int i = 0; i <= A[2]; ++i) printf("%d ", results[i]); }
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 | #include <cstdio> #include <cstring> #include <deque> #include <array> #include <set> using namespace std; #define NMAX 100000 int results[NMAX+10]; int main() { int A[3], a[3]; int *b = a + 1, *c = a+2, *B = A+1, *C = A+2; int sum; scanf("%d%d%d%d%d%d", A, B, C, a, b, c); sum = *a + *b + *c; memset(&results, 0xff, sizeof(int)*(*C+1)); deque<array<int, 3>> queue; queue.push_back({*a, *b, 0}); array<int, 3> cur, nw; set<pair<int, int>> visited; int t; visited.emplace(*a, *b); while(!queue.empty()) { cur = queue.front(); queue.pop_front(); t = cur[2]; cur[2] = sum - cur[1] - cur[0]; for (int k = 0; k<3; ++k) { if (results[cur[k]] == -1) { results[cur[k]] = t; } } //printf("%d %d %d, %d\n", cur[0], cur[1], cur[2], t); for (int src = 0; src < 3; ++src) { for (int j = 1; j <= 2; ++j) { int dst = (src + j)%3; int unt = (src + (j^3))%3; if (cur[src] > 0 && cur[dst] < A[dst]) { nw[unt] = cur[unt]; int tmpsum = cur[src] + cur[dst]; if (tmpsum <= A[dst]) { nw[src] = 0; nw[dst] = tmpsum; } else { nw[dst] = A[dst]; nw[src] = tmpsum - A[dst]; } if (visited.count(make_pair(nw[0], nw[1])) == 0) { visited.emplace(nw[0], nw[1]); queue.push_back({nw[0], nw[1], t+1}); } } } } } for (int i = 0; i <= A[2]; ++i) printf("%d ", results[i]); } |