#include <iostream> #include <map> #include <queue> using namespace std; struct state { long l[3]; }; long id_from_state(long l_a, long l_b, long l_c) { return l_a * 100001 + l_b; } state state_from_id(long id, long total_l) { state s; s.l[1] = id % 100001; s.l[0] = id / 100001; s.l[2] = total_l - s.l[1] - s.l[0]; return s; } pair<long, long> from_to(long l_a, long v_a, long l_b, long v_b) { pair<long, long> p; long total_l = l_a + l_b; if (total_l <= v_b) { p.first = 0; p.second = total_l; } else { p.second = v_b; p.first = total_l - v_b; } return p; } // from, to: // 0 - a // 1 - b // 2 - c void handle_case(int from, int to, int unchanged, long *vs, state s, long total_l, map<long, bool> &visited, queue<pair<long, long>> &next, long next_depth) { pair<long, long> p; long new_id; long new_depth; pair<long, long> new_state; // state xx; state new_vals; p = from_to(s.l[from], vs[from], s.l[to], vs[to]); new_vals.l[from] = p.first; new_vals.l[to] = p.second; new_vals.l[unchanged] = s.l[unchanged]; new_id = id_from_state(new_vals.l[0], new_vals.l[1], new_vals.l[2]); // xx = state_from_id(new_id, total_l); // cerr << "trying " << xx.l[0] << " " << xx.l[1] << " " << xx.l[2] << endl; new_depth = next_depth + 1; new_state = pair<long, long>(new_id, new_depth); if (!visited[new_id]) { visited[new_id] = true; // cerr << "PUSHING " << new_state.second << ": " << new_state.first << endl; next.push(new_state); } } int main() { long v[3]; long l[3]; long total_l; map<long, bool> visited; queue<pair<long, long>> next; long possible[100001]; cin >> v[0] >> v[1] >> v[2]; cin >> l[0] >> l[1] >> l[2]; for (int i = 0; i <= v[2]; ++i) { possible[i] = -1; } total_l = l[0] + l[1] + l[2]; long stateid = l[0] * 100001 + l[1]; visited[stateid] = true; pair<long, long> first_state; first_state.first = stateid; first_state.second = 0; next.push(first_state); while(!next.empty()) { pair<long, long> next_state = next.front(); next.pop(); // cerr << "GETTING " << next_state.second << ": " << next_state.first << endl; long next_id = next_state.first; long next_depth = next_state.second; state s = state_from_id(next_id, total_l); // cerr << "DEPTH: " << next_depth <<" , STATE: " << s.l[0] << " " << s.l[1] << " " << s.l[2] << endl; if (possible[s.l[0]] == -1) { possible[s.l[0]] = next_depth; } if (possible[s.l[1]] == -1) { possible[s.l[1]] = next_depth; } if (possible[s.l[2]] == -1) { possible[s.l[2]] = next_depth; } // A -> B handle_case(0, 1, 2, v, s, total_l, visited, next, next_depth); // B -> A handle_case(1, 0, 2, v, s, total_l, visited, next, next_depth); // A -> C handle_case(0, 2, 1, v, s, total_l, visited, next, next_depth); // C -> A handle_case(2, 0, 1, v, s, total_l, visited, next, next_depth); // B -> C handle_case(1, 2, 0, v, s, total_l, visited, next, next_depth); // C -> B handle_case(2, 1, 0, v, s, total_l, visited, next, next_depth); } for (int i = 0; i <= v[2]; ++i) { cout << possible[i] << " "; } 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 | #include <iostream> #include <map> #include <queue> using namespace std; struct state { long l[3]; }; long id_from_state(long l_a, long l_b, long l_c) { return l_a * 100001 + l_b; } state state_from_id(long id, long total_l) { state s; s.l[1] = id % 100001; s.l[0] = id / 100001; s.l[2] = total_l - s.l[1] - s.l[0]; return s; } pair<long, long> from_to(long l_a, long v_a, long l_b, long v_b) { pair<long, long> p; long total_l = l_a + l_b; if (total_l <= v_b) { p.first = 0; p.second = total_l; } else { p.second = v_b; p.first = total_l - v_b; } return p; } // from, to: // 0 - a // 1 - b // 2 - c void handle_case(int from, int to, int unchanged, long *vs, state s, long total_l, map<long, bool> &visited, queue<pair<long, long>> &next, long next_depth) { pair<long, long> p; long new_id; long new_depth; pair<long, long> new_state; // state xx; state new_vals; p = from_to(s.l[from], vs[from], s.l[to], vs[to]); new_vals.l[from] = p.first; new_vals.l[to] = p.second; new_vals.l[unchanged] = s.l[unchanged]; new_id = id_from_state(new_vals.l[0], new_vals.l[1], new_vals.l[2]); // xx = state_from_id(new_id, total_l); // cerr << "trying " << xx.l[0] << " " << xx.l[1] << " " << xx.l[2] << endl; new_depth = next_depth + 1; new_state = pair<long, long>(new_id, new_depth); if (!visited[new_id]) { visited[new_id] = true; // cerr << "PUSHING " << new_state.second << ": " << new_state.first << endl; next.push(new_state); } } int main() { long v[3]; long l[3]; long total_l; map<long, bool> visited; queue<pair<long, long>> next; long possible[100001]; cin >> v[0] >> v[1] >> v[2]; cin >> l[0] >> l[1] >> l[2]; for (int i = 0; i <= v[2]; ++i) { possible[i] = -1; } total_l = l[0] + l[1] + l[2]; long stateid = l[0] * 100001 + l[1]; visited[stateid] = true; pair<long, long> first_state; first_state.first = stateid; first_state.second = 0; next.push(first_state); while(!next.empty()) { pair<long, long> next_state = next.front(); next.pop(); // cerr << "GETTING " << next_state.second << ": " << next_state.first << endl; long next_id = next_state.first; long next_depth = next_state.second; state s = state_from_id(next_id, total_l); // cerr << "DEPTH: " << next_depth <<" , STATE: " << s.l[0] << " " << s.l[1] << " " << s.l[2] << endl; if (possible[s.l[0]] == -1) { possible[s.l[0]] = next_depth; } if (possible[s.l[1]] == -1) { possible[s.l[1]] = next_depth; } if (possible[s.l[2]] == -1) { possible[s.l[2]] = next_depth; } // A -> B handle_case(0, 1, 2, v, s, total_l, visited, next, next_depth); // B -> A handle_case(1, 0, 2, v, s, total_l, visited, next, next_depth); // A -> C handle_case(0, 2, 1, v, s, total_l, visited, next, next_depth); // C -> A handle_case(2, 0, 1, v, s, total_l, visited, next, next_depth); // B -> C handle_case(1, 2, 0, v, s, total_l, visited, next, next_depth); // C -> B handle_case(2, 1, 0, v, s, total_l, visited, next, next_depth); } for (int i = 0; i <= v[2]; ++i) { cout << possible[i] << " "; } cout << endl; return 0; } |