Advertisement
VRonin

Box Split

Jul 7th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5.  
  6. bool BestApprox(int BoxSizes[],int dim,int Approx1[],int Approx2[],int n){
  7.     int res1=0,res2=0;
  8.     for (int i=0;i<dim;i++){
  9.         res1+=Approx1[i]*BoxSizes[i];
  10.         res2+=Approx2[i]*BoxSizes[i];
  11.     }
  12.     if (res1>n) return false;
  13.     return res1>res2;
  14. }
  15.  
  16. bool FillBoxes( //returns true if n items perfectly fits int the boxes
  17.                int BoxSizes[], //an array containing the box sizes
  18.                int dim, //the dimension of the array BoxSizes
  19.                int Used[], //an array of size dim that will be filled with the number of boxes used. if the function returns false this contains junk
  20.                int Approx[], //an array of size dim that will be filled with the number of boxes used that best approximates the number of items (it will be equal to Used if the fit is perfect)
  21.                int n //number of items to divide in boxes
  22.                ){
  23.                    int temp=0;
  24.                    for (int i=0;i<dim;i++) temp+=Used[i]*BoxSizes[i];
  25.                    if (n-temp<0) return false;
  26.                    for (int i=0;i<dim;i++){
  27.                        if((n-temp)%BoxSizes[i]==0){
  28.                            Used[i]+=(n-temp)/BoxSizes[i];
  29.                            Approx=Used;
  30.                            return true;
  31.                        }
  32.                    }
  33.                    for (int i=0;i<dim;i++){
  34.                        Used[i]+=1;
  35.                        if(FillBoxes(BoxSizes,dim,Used,Approx,n)) return true;
  36.                        if (BestApprox(BoxSizes,dim,Used,Approx,n)){
  37.                            for (int i=0;i<dim;i++) Approx[i]=Used[i];
  38.                        }
  39.                        Used[i]-=1;
  40.                    }
  41.                    return false;
  42. }
  43.  
  44. int main(){
  45.     int Sizes[3]={20,9,6}; //sizes of the nuggets containers
  46.     int BoxCombination[3]={0,0,0}; //array of zeros that will be filled with the solution
  47.     int BoxApprox[3]={0,0,0}; //array of zeros that will be filled with the best appoximation of the solution if the solution can't be found
  48.     int items; //number of nuggets to split
  49.     cout << "How many nuggets do you want to buy? "; cin >> items;
  50.     if (FillBoxes(Sizes,3,BoxCombination,BoxApprox,items)){
  51.         cout << endl << "The items perfectly fit in:";
  52.         for (int i=0;i<3;i++){
  53.             cout << endl << BoxCombination[i] << " Boxes of size " << Sizes[i];
  54.         }
  55.         cout << endl;
  56.     }
  57.     else{
  58.         int temp=0;
  59.         cout << endl << "The items don't perfectly fit in the boxes."<<endl<<"The best appoximation is:";
  60.         for (int i=0;i<3;i++){
  61.             cout << endl << BoxApprox[i] << " Boxes of size " << Sizes[i];
  62.             temp+=BoxApprox[i]*Sizes[i];
  63.         }
  64.         cout << endl << "That fits exactly " << temp << " items" << endl;
  65.     }
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement