#include <iostream>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
int A, B, C, a, b, c;
void wczytaj(){
scanf("%d %d %d", &A, &B, &C);
scanf("%d %d %d", &a, &b, &c);
}
struct stan{
int x, y, z;
bool operator<(const stan& Prawy) const{
if(x<Prawy.x) return 1;
if(x>Prawy.x) return 0;
if(y<Prawy.y) return 1;
if(y>Prawy.y) return 0;
return z<Prawy.z;
}
stan opBA(){
stan zwrot;
zwrot.x=min(A, x+y);
zwrot.y=x+y-zwrot.x;
zwrot.z=z;
return zwrot;
}
stan opAB(){
stan zwrot;
zwrot.y=min(B, x+y);
zwrot.x=x+y-zwrot.y;
zwrot.z=z;
return zwrot;
}
stan opCA(){
stan zwrot;
zwrot.x=min(A, x+z);
zwrot.y=y;
zwrot.z=x+z-zwrot.x;
return zwrot;
}
stan opAC(){
stan zwrot;
zwrot.z = min(C, x+z);
zwrot.y=y;
zwrot.x=x+z-zwrot.z;
return zwrot;
}
stan opCB(){
stan zwrot;
zwrot.y=min(B, y+z);
zwrot.x=x;
zwrot.z = y+z-zwrot.y;
return zwrot;
}
stan opBC(){
stan zwrot;
zwrot.z=min(C, y+z);
zwrot.x=x;
zwrot.y=y+z-zwrot.z;
return zwrot;
}
};
map<stan, int> odleglosci;
int * najkrocej;
void rozw(){
najkrocej = new int[C+1];
int odl=0;
stan stpom;
stpom.x=a;
stpom.y=b;
stpom.z=c;
stack<stan> * wej = new stack<stan>;
stack<stan> * wyj = new stack<stan>;
wej->push(stpom);
while(!wej->empty()){
while(!wej->empty()){
if(odleglosci.count(wej->top())==0){
odleglosci[wej->top()] = odl;
wyj->push((wej->top()).opAB());
wyj->push((wej->top()).opBA());
wyj->push((wej->top()).opAC());
wyj->push((wej->top()).opCA());
wyj->push((wej->top()).opBC());
wyj->push((wej->top()).opCB());
}
wej->pop();
}
odl++;
stack<stan> * wskpom = wej;
wej=wyj;
wyj=wskpom;
}
for(int i=0; i<=C; i++) najkrocej[i]=1000000000;
for(auto it=odleglosci.begin(); it!=odleglosci.end(); it++){
stpom = it->first;
odl = it->second;
najkrocej[stpom.x] = min(najkrocej[stpom.x], odl);
najkrocej[stpom.y] = min(najkrocej[stpom.y], odl);
najkrocej[stpom.z] = min(najkrocej[stpom.z], odl);
}
for(int i=0; i<=C; i++)
if(najkrocej[i]==1000000000) printf("-1 "); else
printf("%d ", najkrocej[i]);
}
int main()
{
wczytaj();
rozw();
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 | #include <iostream> #include <fstream> #include <map> #include <stack> using namespace std; int A, B, C, a, b, c; void wczytaj(){ scanf("%d %d %d", &A, &B, &C); scanf("%d %d %d", &a, &b, &c); } struct stan{ int x, y, z; bool operator<(const stan& Prawy) const{ if(x<Prawy.x) return 1; if(x>Prawy.x) return 0; if(y<Prawy.y) return 1; if(y>Prawy.y) return 0; return z<Prawy.z; } stan opBA(){ stan zwrot; zwrot.x=min(A, x+y); zwrot.y=x+y-zwrot.x; zwrot.z=z; return zwrot; } stan opAB(){ stan zwrot; zwrot.y=min(B, x+y); zwrot.x=x+y-zwrot.y; zwrot.z=z; return zwrot; } stan opCA(){ stan zwrot; zwrot.x=min(A, x+z); zwrot.y=y; zwrot.z=x+z-zwrot.x; return zwrot; } stan opAC(){ stan zwrot; zwrot.z = min(C, x+z); zwrot.y=y; zwrot.x=x+z-zwrot.z; return zwrot; } stan opCB(){ stan zwrot; zwrot.y=min(B, y+z); zwrot.x=x; zwrot.z = y+z-zwrot.y; return zwrot; } stan opBC(){ stan zwrot; zwrot.z=min(C, y+z); zwrot.x=x; zwrot.y=y+z-zwrot.z; return zwrot; } }; map<stan, int> odleglosci; int * najkrocej; void rozw(){ najkrocej = new int[C+1]; int odl=0; stan stpom; stpom.x=a; stpom.y=b; stpom.z=c; stack<stan> * wej = new stack<stan>; stack<stan> * wyj = new stack<stan>; wej->push(stpom); while(!wej->empty()){ while(!wej->empty()){ if(odleglosci.count(wej->top())==0){ odleglosci[wej->top()] = odl; wyj->push((wej->top()).opAB()); wyj->push((wej->top()).opBA()); wyj->push((wej->top()).opAC()); wyj->push((wej->top()).opCA()); wyj->push((wej->top()).opBC()); wyj->push((wej->top()).opCB()); } wej->pop(); } odl++; stack<stan> * wskpom = wej; wej=wyj; wyj=wskpom; } for(int i=0; i<=C; i++) najkrocej[i]=1000000000; for(auto it=odleglosci.begin(); it!=odleglosci.end(); it++){ stpom = it->first; odl = it->second; najkrocej[stpom.x] = min(najkrocej[stpom.x], odl); najkrocej[stpom.y] = min(najkrocej[stpom.y], odl); najkrocej[stpom.z] = min(najkrocej[stpom.z], odl); } for(int i=0; i<=C; i++) if(najkrocej[i]==1000000000) printf("-1 "); else printf("%d ", najkrocej[i]); } int main() { wczytaj(); rozw(); return 0; } |
English