#include <bits/stdc++.h> using namespace std; int main() { int A,B,C,a0,b0,c0; cin >> A >> B >> C >> a0 >> b0 >> c0; int unknown = C+1; vector<int> res(C+1, INT_MAX); res[a0] = res[b0] = res[c0] = 0; set<tuple<int,int,int>> visited; visited.insert(make_tuple(a0,b0,c0)); queue<tuple<int,int,int,int>> frontier; frontier.push(make_tuple(a0,b0,c0,0)); while(!frontier.empty()) { //cout << "frontier_size = " << frontier.size() << endl; //cout << "visited_size = " << visited.size() << endl; auto x = frontier.front(); // unpack x int a = get<0>(x); int b = get<1>(x); int c = get<2>(x); int d = get<3>(x); //cout << "front: (" << a << "," << b << "," << c << "," << d << ")" << endl; // wolne miejsce int wa = A-a; int wb = B-b; int wc = C-c; int ap[6], bp[6], cp[6], z; // a do b z = min(a,wb); ap[0] = a - z; bp[0] = b + z; cp[0] = c; // a do c z = min(a,wc); ap[1] = a - z; bp[1] = b; cp[1] = c + z; // b do a z = min(b,wa); ap[2] = a + z; bp[2] = b - z; cp[2] = c; // b do c z = min(b,wc); ap[3] = a; bp[3] = b - z; cp[3] = c + z; // c do a z = min(c,wa); ap[4] = a + z; bp[4] = b; cp[4] = c - z; // c do b z = min(c,wb); ap[5] = a; bp[5] = b + z; cp[5] = c - z; for(int i=0; i<6; ++i) if(visited.count(make_tuple(ap[i],bp[i],cp[i]))==0) { visited.insert(make_tuple(ap[i],bp[i],cp[i])); frontier.push(make_tuple(ap[i],bp[i],cp[i],d+1)); if(res[ap[i]]==INT_MAX) { res[ap[i]] = d+1; --unknown; } if(res[bp[i]]==INT_MAX) { res[bp[i]] = d+1; --unknown; } if(res[cp[i]]==INT_MAX) { res[cp[i]] = d+1; --unknown; } //res[ap[i]] = min(res[ap[i]], d+1); //res[bp[i]] = min(res[bp[i]], d+1); //res[cp[i]] = min(res[cp[i]], d+1); } if(unknown==0) break; frontier.pop(); } for(int i=0; i<=C; ++i) cout << (res[i]==INT_MAX?-1:res[i]) << " "; cout << endl; }
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 | #include <bits/stdc++.h> using namespace std; int main() { int A,B,C,a0,b0,c0; cin >> A >> B >> C >> a0 >> b0 >> c0; int unknown = C+1; vector<int> res(C+1, INT_MAX); res[a0] = res[b0] = res[c0] = 0; set<tuple<int,int,int>> visited; visited.insert(make_tuple(a0,b0,c0)); queue<tuple<int,int,int,int>> frontier; frontier.push(make_tuple(a0,b0,c0,0)); while(!frontier.empty()) { //cout << "frontier_size = " << frontier.size() << endl; //cout << "visited_size = " << visited.size() << endl; auto x = frontier.front(); // unpack x int a = get<0>(x); int b = get<1>(x); int c = get<2>(x); int d = get<3>(x); //cout << "front: (" << a << "," << b << "," << c << "," << d << ")" << endl; // wolne miejsce int wa = A-a; int wb = B-b; int wc = C-c; int ap[6], bp[6], cp[6], z; // a do b z = min(a,wb); ap[0] = a - z; bp[0] = b + z; cp[0] = c; // a do c z = min(a,wc); ap[1] = a - z; bp[1] = b; cp[1] = c + z; // b do a z = min(b,wa); ap[2] = a + z; bp[2] = b - z; cp[2] = c; // b do c z = min(b,wc); ap[3] = a; bp[3] = b - z; cp[3] = c + z; // c do a z = min(c,wa); ap[4] = a + z; bp[4] = b; cp[4] = c - z; // c do b z = min(c,wb); ap[5] = a; bp[5] = b + z; cp[5] = c - z; for(int i=0; i<6; ++i) if(visited.count(make_tuple(ap[i],bp[i],cp[i]))==0) { visited.insert(make_tuple(ap[i],bp[i],cp[i])); frontier.push(make_tuple(ap[i],bp[i],cp[i],d+1)); if(res[ap[i]]==INT_MAX) { res[ap[i]] = d+1; --unknown; } if(res[bp[i]]==INT_MAX) { res[bp[i]] = d+1; --unknown; } if(res[cp[i]]==INT_MAX) { res[cp[i]] = d+1; --unknown; } //res[ap[i]] = min(res[ap[i]], d+1); //res[bp[i]] = min(res[bp[i]], d+1); //res[cp[i]] = min(res[cp[i]], d+1); } if(unknown==0) break; frontier.pop(); } for(int i=0; i<=C; ++i) cout << (res[i]==INT_MAX?-1:res[i]) << " "; cout << endl; } |