#include<cstdio>
#include<queue>
#include<set>
using namespace std;
static int poj[3];
struct solution{
int tab[3];
bool operator<(solution a)const{
if(tab[0] != a.tab[0]){
return tab[0] < a.tab[0];
}
if(tab[1] != a.tab[1]){
return tab[1] < a.tab[1];
}
return tab[2] < a.tab[2];
}
};
struct el{
solution s;
int d;
};
static queue<el> kolejka;
static set<solution>zbior;
static int result[100001];
int main(){
for(int i = 0; i < 3; ++i){
scanf("%i", poj + i);
}
for(int i = 0; i <= poj[2]; ++i){
result[i] = -1;
}
solution curr;
for(int i = 0; i < 3; ++i){
scanf("%i", curr.tab + i);
}
el e;
e.s = curr;
e.d = 0;
kolejka.push(e);
zbior.insert(e.s);
while(!kolejka.empty()){
e = kolejka.front();
kolejka.pop();
for(int i = 0; i < 3; ++i){
int ile = e.s.tab[i];
if(result[ile] == -1){
result[ile] = e.d;
}
}
for(int i = 0; i < 3; ++i){
for(int j = 0; j < 3; ++j){
if(i == j){
continue;
}
el e2;
e2.s = e.s;
e2.d = e.d + 1;
e2.s.tab[j] += e2.s.tab[i];
if(e2.s.tab[j] > poj[j]){
e2.s.tab[i] = e2.s.tab[j] - poj[j];
e2.s.tab[j] = poj[j];
}
else{
e2.s.tab[i] = 0;
}
if(zbior.find(e2.s) == zbior.end()){
kolejka.push(e2);
zbior.insert(e2.s);
}
}
}
}
for(int i = 0; i <= poj[2]; ++i){
printf("%i ", result[i]);
}
puts("");
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 | #include<cstdio> #include<queue> #include<set> using namespace std; static int poj[3]; struct solution{ int tab[3]; bool operator<(solution a)const{ if(tab[0] != a.tab[0]){ return tab[0] < a.tab[0]; } if(tab[1] != a.tab[1]){ return tab[1] < a.tab[1]; } return tab[2] < a.tab[2]; } }; struct el{ solution s; int d; }; static queue<el> kolejka; static set<solution>zbior; static int result[100001]; int main(){ for(int i = 0; i < 3; ++i){ scanf("%i", poj + i); } for(int i = 0; i <= poj[2]; ++i){ result[i] = -1; } solution curr; for(int i = 0; i < 3; ++i){ scanf("%i", curr.tab + i); } el e; e.s = curr; e.d = 0; kolejka.push(e); zbior.insert(e.s); while(!kolejka.empty()){ e = kolejka.front(); kolejka.pop(); for(int i = 0; i < 3; ++i){ int ile = e.s.tab[i]; if(result[ile] == -1){ result[ile] = e.d; } } for(int i = 0; i < 3; ++i){ for(int j = 0; j < 3; ++j){ if(i == j){ continue; } el e2; e2.s = e.s; e2.d = e.d + 1; e2.s.tab[j] += e2.s.tab[i]; if(e2.s.tab[j] > poj[j]){ e2.s.tab[i] = e2.s.tab[j] - poj[j]; e2.s.tab[j] = poj[j]; } else{ e2.s.tab[i] = 0; } if(zbior.find(e2.s) == zbior.end()){ kolejka.push(e2); zbior.insert(e2.s); } } } } for(int i = 0; i <= poj[2]; ++i){ printf("%i ", result[i]); } puts(""); return 0; } |
English