#include <bits/stdc++.h>
using namespace std;
int ogr[3];
int wynik[100*1000+5];
set <tuple<int,int,int> > s;
struct syt{
int t[3], nr;
syt(int a, int b, int c, int nr){
this->t[0]=a;
this->t[1]=b;
this->t[2]=c;
this->nr=nr;
}
void wypisz(){
cout << this->t[0]<<" "<<this->t[1]<<" "<<this->t[2]<<" "<<nr<<endl;
}
};
pair <int,int> przelej(int a, int b, int ogr){
if (a+b>=ogr){
return make_pair(a-(ogr-b),ogr);
}
if (a+b<ogr){
return make_pair(0,b+a);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t[3];
cin >> ogr[0] >>ogr[1] >> ogr[2];
cin >> t[0]>>t[1]>>t[2];
queue<syt> q;
q.push(syt(t[0],t[1],t[2],0));
for (int i=0; i<=ogr[2];i++){
wynik[i]=-1;
}
wynik[t[0]]=0;
wynik[t[1]]=0;
wynik[t[2]]=0;
int maxnr=0;
while(!q.empty()){
syt tmp = q.front();
q.pop();
bool czek = 0;
//tmp.wypisz();
maxnr=tmp.nr;
for (int i=0; i<3;i++){
for (int j=0;j<3;j++){
if (i!=j){
syt a(tmp.t[0],tmp.t[1],tmp.t[2],tmp.nr);
//cout << i<<" "<<j<<" ";
//a.wypisz();
pair<int,int> prz = przelej(tmp.t[i],tmp.t[j], ogr[j]);
a.t[i]=prz.first;
a.t[j]=prz.second;
a.nr=tmp.nr+1;
for (int i=0; i<3;i++){
if (wynik[a.t[i]] == -1){
wynik[a.t[i]]=a.nr;
}
}
//a.wypisz();
if (s.find(make_tuple(a.t[0],a.t[1],a.t[2]))==s.end()){
s.insert(make_tuple(a.t[0],a.t[1],a.t[2]));
//cout << "wrz"<<endl;
q.push(a);
}
//cout << endl;
}
}
}
}
for (int i=0;i<=ogr[2];i++){
cout << wynik[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 | #include <bits/stdc++.h> using namespace std; int ogr[3]; int wynik[100*1000+5]; set <tuple<int,int,int> > s; struct syt{ int t[3], nr; syt(int a, int b, int c, int nr){ this->t[0]=a; this->t[1]=b; this->t[2]=c; this->nr=nr; } void wypisz(){ cout << this->t[0]<<" "<<this->t[1]<<" "<<this->t[2]<<" "<<nr<<endl; } }; pair <int,int> przelej(int a, int b, int ogr){ if (a+b>=ogr){ return make_pair(a-(ogr-b),ogr); } if (a+b<ogr){ return make_pair(0,b+a); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t[3]; cin >> ogr[0] >>ogr[1] >> ogr[2]; cin >> t[0]>>t[1]>>t[2]; queue<syt> q; q.push(syt(t[0],t[1],t[2],0)); for (int i=0; i<=ogr[2];i++){ wynik[i]=-1; } wynik[t[0]]=0; wynik[t[1]]=0; wynik[t[2]]=0; int maxnr=0; while(!q.empty()){ syt tmp = q.front(); q.pop(); bool czek = 0; //tmp.wypisz(); maxnr=tmp.nr; for (int i=0; i<3;i++){ for (int j=0;j<3;j++){ if (i!=j){ syt a(tmp.t[0],tmp.t[1],tmp.t[2],tmp.nr); //cout << i<<" "<<j<<" "; //a.wypisz(); pair<int,int> prz = przelej(tmp.t[i],tmp.t[j], ogr[j]); a.t[i]=prz.first; a.t[j]=prz.second; a.nr=tmp.nr+1; for (int i=0; i<3;i++){ if (wynik[a.t[i]] == -1){ wynik[a.t[i]]=a.nr; } } //a.wypisz(); if (s.find(make_tuple(a.t[0],a.t[1],a.t[2]))==s.end()){ s.insert(make_tuple(a.t[0],a.t[1],a.t[2])); //cout << "wrz"<<endl; q.push(a); } //cout << endl; } } } } for (int i=0;i<=ogr[2];i++){ cout << wynik[i]<<" "; } cout << endl; } |
English