(file) Return to acm2.cpp CVS log (file) (dir) Up to [RizwankCVS] / acm

Diff for /acm/acm2.cpp between version 1.11 and 1.12

version 1.11, 2005/02/16 10:04:23 version 1.12, 2005/02/16 10:27:53
Line 3 
Line 3 
 #include<fstream> #include<fstream>
 #include<string> #include<string>
 #include <queue> #include <queue>
   #include <stack>
 #include <stdio.h> #include <stdio.h>
 // yes I could use just one i/o lib // yes I could use just one i/o lib
 using namespace std; using namespace std;
Line 15 
Line 16 
 // their parent object was in the priority queue, so static sized // their parent object was in the priority queue, so static sized
 // arrays were used // arrays were used
  
 #define MAX_ITEMS 7000  #define MAX_ITEMS 65536
   //the above bound comes from the fixed value for the boolean array TODO REMOVE LIMITATION
  
 typedef long ITEM_MASS; typedef long ITEM_MASS;
 typedef long INDEX_TYPE; typedef long INDEX_TYPE;
Line 393 
Line 395 
         }         }
  
  
                 printf("result : %d\n",temp_key.getWeight());          if ( temp_key.getWeight() == targetvalue ) {
   
   
   
                 int totalitemsinserted = 0;                 int totalitemsinserted = 0;
                 int first=1;                 int first=1;
                 for (int i = 0; this->totalItems > i; i++) {                 for (int i = 0; this->totalItems > i; i++) {
Line 406 
Line 405 
                                 printf("%d", this->item_array[i].getWeight());                                 printf("%d", this->item_array[i].getWeight());
                         }                         }
                 }                 }
           }
           else { printf("CANNOT FILL PALLET\n"); }
 } }
  
 /* /*
Line 477 
Line 477 
  
         char * read_buffer = (char *) malloc (length);         char * read_buffer = (char *) malloc (length);
         ITEM_MASS * inventory = (ITEM_MASS * ) malloc (length);         ITEM_MASS * inventory = (ITEM_MASS * ) malloc (length);
                   // could use stack instead of array --- doesn't require malloc, more 'safe' avoids overflows, esp if
                   // input file is funky --- its better software engineering, but its slower. Given that this is a pretty
                   // small system, I'll stick with the array
         inputfs.getline (read_buffer, length-1);         inputfs.getline (read_buffer, length-1);
  
         char * theToken; INDEX_TYPE index = 0;         char * theToken; INDEX_TYPE index = 0;
Line 492 
Line 495 
  
         INDEX_TYPE size_inventory = index; //remember this is 1 based, not 0         INDEX_TYPE size_inventory = index; //remember this is 1 based, not 0
  
           int SUPER_MODE = 0;
           if (inventory[0]==1) { SUPER_MODE=0;}
   
         if (DEBUG_MODE) {         if (DEBUG_MODE) {
                 printf("Line 1 read - %d products from warehouse\n", size_inventory);                 printf("Line 1 read - %d products from warehouse\n", size_inventory);
                   if (SUPER_MODE) printf("Working in Superincreasing mode!\n");
         }         }
  
 //      qsort ( inventory, size_inventory, sizeof(int), intcomp);       // we now have a sorted list //      qsort ( inventory, size_inventory, sizeof(int), intcomp);       // we now have a sorted list
Line 506 
Line 513 
         }         }
  
         inputfs.close();         inputfs.close();
           free(read_buffer);
   
           if
   
  
         // ckeanup, release memeory, close files here         // ckeanup, release memeory, close files here
  
Line 527 
Line 538 
  
         }         }
         knapsackOne.store_item_array();         knapsackOne.store_item_array();
           free(inventory);
         knapsackOne.branch_and_bound(DEBUG_MODE,palette_size);         knapsackOne.branch_and_bound(DEBUG_MODE,palette_size);
  
         printf("\n");         printf("\n");
 } }
  
  
   
   
   void super ( ITEM_MASS target_value , INDEX_TYPE size_inventory, int DEBUG_MODE ) {
           // input, a super increasing set, lowest to highest.
           // the number of elements in the list [1-ordinal]
           // a target value.
           //debugmode
   
           // need to output values that sum to target
           // need another place to store. --- not another array, need LIFO structure. A stack of course.
           // we have to count downwards during super increasing, or we could qsort the list. (not going to do that)
   
           stack<ITEM_MASS> packages_to_load;
   
           if (DEBUG_MODE) {
                   printf("Extracting values, highest to lowest, doing reduction\n");
           }
           for ( INDEX_TYPE store_index=size_inventory-1; size_inventory<=0;store_index-- ) {
                   if (DEBUG_MODE) {
                           printf("value %d, is it smaller than %d? If so, subtract and store.\n",inventory[store_index],target_value);
                   }
                   if ( inventory[store_index] <= target_value ) {
                           target_value = target_value - inventory[store_index];
                           packages_to_load.push(inventory[store_index]);
                   }
           }
           if ( target_value == 0 ) {
                   int first = 1;
                   while ( ! packages_to_load.empty() ) {
                           if (first==0) { printf(","); }
                           else {first = 0;}                     // there has GOT to be a better way!
                           printf("%d",packages_to_load.top());
                           packages_to_load.pop();
                   }
                   else { printf("CANNOT FILL PALLET\n"); }
   
   
           }
   
   
   
   
 /* /*
 Modifications to the original branch-and-bound algorithim/approach Modifications to the original branch-and-bound algorithim/approach
  


Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

Rizwan Kassim
Powered by
ViewCVS 0.9.2