#include <bits/stdc++.h> using namespace std; #define FOR(ii, ll, uu) for(int ii##lim = (uu), ii = (ll); ii < ii##lim; ++ii) #define REP(ii, nn) FOR(ii, 0, nn) typedef tuple<int, int, int> tri; int main() { int A, B, C, a, b, c; cin >> A >> B >> C >> a >> b >> c; vector<int> d(C+1, -1); d[a] = d[b] = d[c] = 0; cout << d[0] << endl; vector<tri> layer, new_layer; set<tri> seen; seen.insert(make_tuple(a, b, c)); layer.push_back(make_tuple(a, b, c)); int layer_id = 1; while(!layer.empty()) { for (tri& t : layer) { int aa, bb, cc; tie(aa, bb, cc) = t; if (aa > 0) { if (bb < B) { int amount = min(aa, B - bb); tri x = make_tuple(aa-amount, bb+amount, cc); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } if (cc < C) { int amount = min(aa, C - cc); tri x = make_tuple(aa-amount, bb, cc+amount); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } } if (bb > 0) { if (aa < A) { int amount = min(bb, A - aa); tri x = make_tuple(aa+amount, bb-amount, cc); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } if (cc < C) { int amount = min(bb, C - cc); tri x = make_tuple(aa, bb-amount, cc+amount); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } } if (cc > 0) { if (aa < A) { int amount = min(cc, A - aa); tri x = make_tuple(aa+amount, bb, cc-amount); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } if (bb < B) { int amount = min(cc, B - bb); tri x = make_tuple(aa, bb+amount, cc-amount); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } } } for (tri &t : new_layer) { int aa, bb, cc; tie(aa, bb, cc) = t; if (d[aa] == -1) d[aa] = layer_id; if (d[bb] == -1) d[bb] = layer_id; if (d[cc] == -1) d[cc] = layer_id; } swap(layer, new_layer); new_layer.clear(); ++layer_id; } cout << d[0]; for (int i=0; i<C; ++i) { cout << ' ' << d[i+1]; } 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 | #include <bits/stdc++.h> using namespace std; #define FOR(ii, ll, uu) for(int ii##lim = (uu), ii = (ll); ii < ii##lim; ++ii) #define REP(ii, nn) FOR(ii, 0, nn) typedef tuple<int, int, int> tri; int main() { int A, B, C, a, b, c; cin >> A >> B >> C >> a >> b >> c; vector<int> d(C+1, -1); d[a] = d[b] = d[c] = 0; cout << d[0] << endl; vector<tri> layer, new_layer; set<tri> seen; seen.insert(make_tuple(a, b, c)); layer.push_back(make_tuple(a, b, c)); int layer_id = 1; while(!layer.empty()) { for (tri& t : layer) { int aa, bb, cc; tie(aa, bb, cc) = t; if (aa > 0) { if (bb < B) { int amount = min(aa, B - bb); tri x = make_tuple(aa-amount, bb+amount, cc); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } if (cc < C) { int amount = min(aa, C - cc); tri x = make_tuple(aa-amount, bb, cc+amount); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } } if (bb > 0) { if (aa < A) { int amount = min(bb, A - aa); tri x = make_tuple(aa+amount, bb-amount, cc); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } if (cc < C) { int amount = min(bb, C - cc); tri x = make_tuple(aa, bb-amount, cc+amount); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } } if (cc > 0) { if (aa < A) { int amount = min(cc, A - aa); tri x = make_tuple(aa+amount, bb, cc-amount); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } if (bb < B) { int amount = min(cc, B - bb); tri x = make_tuple(aa, bb+amount, cc-amount); if (seen.count(x) == 0) { new_layer.push_back(x); seen.insert(x); } } } } for (tri &t : new_layer) { int aa, bb, cc; tie(aa, bb, cc) = t; if (d[aa] == -1) d[aa] = layer_id; if (d[bb] == -1) d[bb] = layer_id; if (d[cc] == -1) d[cc] = layer_id; } swap(layer, new_layer); new_layer.clear(); ++layer_id; } cout << d[0]; for (int i=0; i<C; ++i) { cout << ' ' << d[i+1]; } cout << endl; return 0; } |