#include<bits/stdc++.h> using namespace std; int results[100009]; int numberOfPourings = 0; void updateResult(int x, int value) { results[x] = min(results[x],value); } bool tryPour(int & from, int & to, int & toBottle) { int toBePoured = min(from,toBottle-to); if (toBePoured>0) { from-=toBePoured; to+=toBePoured; numberOfPourings++; updateResult(from, numberOfPourings); updateResult(to, numberOfPourings); return true; } return false; } void anotherStrategy1(int from, int middleman, int to, int frombottlem, int middlemanBottle, int toBottle) { tryPour(to,from,frombottlem); tryPour(middleman,to,toBottle); while(tryPour(from,middleman,middlemanBottle) and tryPour(middleman,to,toBottle)); } void anotherStrategy2(int from, int middleman, int to, int frombottlem, int middlemanBottle, int toBottle) { tryPour(middleman,from,frombottlem); while(tryPour(to,middleman,middlemanBottle) and tryPour(middleman,from,frombottlem)); } void tryStrategy(int from, int middleMan, int to, int fromBottle, int middleManBottle, int toBottle) { while(tryPour(from,middleMan,middleManBottle) and tryPour(middleMan,to,toBottle)) { if(to==toBottle) { anotherStrategy1(from, middleMan, to, fromBottle, middleManBottle, toBottle); anotherStrategy2(from, middleMan, to, fromBottle, middleManBottle, toBottle); } } } void tryStrategyWithEmptyingMiddleManFirst(int from, int middleMan, int to, int fromBottle, int middleManBottle, int toBottle) { if(tryPour(middleMan, to, toBottle)) tryStrategy(from, middleMan, to, fromBottle, middleManBottle, toBottle); } void tryStrategyWithEmptyingMiddleManFirstToFromBottle(int from, int middleMan, int to, int fromBottle, int middleManBottle, int toBottle) { if(tryPour(middleMan, from, fromBottle)) tryStrategy(from, middleMan, to, fromBottle, middleManBottle, toBottle); } void tryStrategyWithEmptyingToFirst(int from, int middleMan, int to, int fromBottle, int middleManBottle, int toBottle) { if(tryPour(to, from, fromBottle)) { tryStrategy(from, middleMan, to, fromBottle, middleManBottle, toBottle); numberOfPourings=1; tryStrategyWithEmptyingMiddleManFirst(from, middleMan, to, fromBottle, middleManBottle, toBottle); numberOfPourings=1; tryStrategyWithEmptyingMiddleManFirstToFromBottle(from, middleMan, to, fromBottle, middleManBottle, toBottle); } } void tryFullStrategy(int from, int middleMan, int to, int fromBottle, int middleManBottle, int toBottle) { numberOfPourings=0; tryStrategy(from, middleMan, to, fromBottle,middleManBottle, toBottle); numberOfPourings=0; tryStrategyWithEmptyingMiddleManFirst(from, middleMan, to, fromBottle, middleManBottle, toBottle); numberOfPourings=0; tryStrategyWithEmptyingToFirst(from, middleMan, to, fromBottle, middleManBottle, toBottle); } int main() { int A,B,C,a,b,c; cin>>A>>B>>C>>a>>b>>c; for(int i=0;i<=C;i++) results[i]=1e9; results[a]=results[b]=results[c]=0; // updateResult(min(a+b,A), 1); // updateResult(min(a+b,B), 1); // updateResult(min(a+c,A), 1); // updateResult(min(a+c,C), 1); // updateResult(min(b+c,B), 1); // updateResult(min(b+c,C), 1); /// scenario one: /// 1. pour from three to one /// 2. pour from one to two /// 3. repeat (1. 2.) /// scenario two: /// 1. pour from one to two /// 2. pour from three to one /// 3. pour from one to two /// 4. repeat (2. 3.) tryFullStrategy(c,a,b,C,A,B); tryFullStrategy(b,a,c,B,A,C); tryFullStrategy(c,b,a,C,B,A); tryFullStrategy(a,b,c,A,B,C); tryFullStrategy(a,c,b,A,C,B); tryFullStrategy(b,c,a,B,C,A); /// scenario three: /// 1. pour from two to one /// 2. pour from one to three /// 3. repeat (1. 2.) /// scenario four: /// 1. pour from one to three /// 2. pour from two to one /// 3. pour from one to three /// 4. repeat (2. 3.) for(int i=0;i<=C;i++) cout<<(results[i]==1e9 ? -1 : results[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 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 133 134 135 | #include<bits/stdc++.h> using namespace std; int results[100009]; int numberOfPourings = 0; void updateResult(int x, int value) { results[x] = min(results[x],value); } bool tryPour(int & from, int & to, int & toBottle) { int toBePoured = min(from,toBottle-to); if (toBePoured>0) { from-=toBePoured; to+=toBePoured; numberOfPourings++; updateResult(from, numberOfPourings); updateResult(to, numberOfPourings); return true; } return false; } void anotherStrategy1(int from, int middleman, int to, int frombottlem, int middlemanBottle, int toBottle) { tryPour(to,from,frombottlem); tryPour(middleman,to,toBottle); while(tryPour(from,middleman,middlemanBottle) and tryPour(middleman,to,toBottle)); } void anotherStrategy2(int from, int middleman, int to, int frombottlem, int middlemanBottle, int toBottle) { tryPour(middleman,from,frombottlem); while(tryPour(to,middleman,middlemanBottle) and tryPour(middleman,from,frombottlem)); } void tryStrategy(int from, int middleMan, int to, int fromBottle, int middleManBottle, int toBottle) { while(tryPour(from,middleMan,middleManBottle) and tryPour(middleMan,to,toBottle)) { if(to==toBottle) { anotherStrategy1(from, middleMan, to, fromBottle, middleManBottle, toBottle); anotherStrategy2(from, middleMan, to, fromBottle, middleManBottle, toBottle); } } } void tryStrategyWithEmptyingMiddleManFirst(int from, int middleMan, int to, int fromBottle, int middleManBottle, int toBottle) { if(tryPour(middleMan, to, toBottle)) tryStrategy(from, middleMan, to, fromBottle, middleManBottle, toBottle); } void tryStrategyWithEmptyingMiddleManFirstToFromBottle(int from, int middleMan, int to, int fromBottle, int middleManBottle, int toBottle) { if(tryPour(middleMan, from, fromBottle)) tryStrategy(from, middleMan, to, fromBottle, middleManBottle, toBottle); } void tryStrategyWithEmptyingToFirst(int from, int middleMan, int to, int fromBottle, int middleManBottle, int toBottle) { if(tryPour(to, from, fromBottle)) { tryStrategy(from, middleMan, to, fromBottle, middleManBottle, toBottle); numberOfPourings=1; tryStrategyWithEmptyingMiddleManFirst(from, middleMan, to, fromBottle, middleManBottle, toBottle); numberOfPourings=1; tryStrategyWithEmptyingMiddleManFirstToFromBottle(from, middleMan, to, fromBottle, middleManBottle, toBottle); } } void tryFullStrategy(int from, int middleMan, int to, int fromBottle, int middleManBottle, int toBottle) { numberOfPourings=0; tryStrategy(from, middleMan, to, fromBottle,middleManBottle, toBottle); numberOfPourings=0; tryStrategyWithEmptyingMiddleManFirst(from, middleMan, to, fromBottle, middleManBottle, toBottle); numberOfPourings=0; tryStrategyWithEmptyingToFirst(from, middleMan, to, fromBottle, middleManBottle, toBottle); } int main() { int A,B,C,a,b,c; cin>>A>>B>>C>>a>>b>>c; for(int i=0;i<=C;i++) results[i]=1e9; results[a]=results[b]=results[c]=0; // updateResult(min(a+b,A), 1); // updateResult(min(a+b,B), 1); // updateResult(min(a+c,A), 1); // updateResult(min(a+c,C), 1); // updateResult(min(b+c,B), 1); // updateResult(min(b+c,C), 1); /// scenario one: /// 1. pour from three to one /// 2. pour from one to two /// 3. repeat (1. 2.) /// scenario two: /// 1. pour from one to two /// 2. pour from three to one /// 3. pour from one to two /// 4. repeat (2. 3.) tryFullStrategy(c,a,b,C,A,B); tryFullStrategy(b,a,c,B,A,C); tryFullStrategy(c,b,a,C,B,A); tryFullStrategy(a,b,c,A,B,C); tryFullStrategy(a,c,b,A,C,B); tryFullStrategy(b,c,a,B,C,A); /// scenario three: /// 1. pour from two to one /// 2. pour from one to three /// 3. repeat (1. 2.) /// scenario four: /// 1. pour from one to three /// 2. pour from two to one /// 3. pour from one to three /// 4. repeat (2. 3.) for(int i=0;i<=C;i++) cout<<(results[i]==1e9 ? -1 : results[i] )<<" "; cout<<endl; } |