Advertisement
Guest User

Untitled

a guest
Mar 30th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  1.     #ifndef BACKTRACK_H
  2.     #define BACKTRACK_H
  3.     //a is really not much use in most cases
  4.     //k is the kth move.  Use it if you need to use the move later
  5.     //input is the input data
  6.     //ncandidates is the number of candidates returned at kth move
  7.     //MAXCANDIDATES is the max possible candidate
  8.     //finished is to indicate to continue search or quit.  One may use it to
  9.     //stop when you have got a "solution".  Set it to "false" to run through
  10.     //all possible solutions.
  11.     void make_move(int a[],int k, void *input);
  12.     void unmake_move(int a[], int k, void *input);
  13.     void construct_candidates(int a[], int k, void *input,
  14.                               int c[], int *ncandidates, int MAXCANDIDATES);
  15.     bool is_a_solution(int a[],int k, void *input);
  16.     void process_solution(int a[], int k, void *input, bool *finished);
  17.     void backtrack(int a[], int k, void *input, int MAXCANDIDATES, bool *finished);
  18.     #endif
  19.  
  20.  
  21.  
  22.     //backtrack.cc
  23.      
  24.     #include <cstdlib>
  25.     #include <vector>
  26.     #include <iostream>
  27.     #include "backtrack.h"
  28.     //a is the solution at this point
  29.     //k don't worry!  The first call should be 0
  30.     //input is the input data.  However you want to interpret it
  31.     //MAXCANDIDATES is the max number of candidates at the  move
  32.     //finished is to indicate to continue search or quit.  One may use it to
  33.     //stop when you have got a "solution".  Set it to "false" to run through
  34.     //all possible solutions.
  35.     void backtrack(int a[], int k, void *input, int MAXCANDIDATES, bool *finished)
  36.     {
  37.       int *c=new int[MAXCANDIDATES]; // candidates for next position
  38.       int ncandidates=0; // next position candidate count
  39.       int i;
  40.       if (is_a_solution(a,k,input))
  41.         process_solution(a,k,input,finished);
  42.       else{
  43.         k = k+1;
  44.         construct_candidates(a,k,input,c,&ncandidates,MAXCANDIDATES);
  45.         for (i=0; i<ncandidates; i++) {
  46.           a[k] = c[i];  //run through each candidate
  47.           make_move(a,k,input);
  48.           backtrack(a,k,input,MAXCANDIDATES, finished);
  49.           if (*finished){
  50.             delete []c;
  51.             return; // terminate early
  52.           }
  53.           unmake_move(a,k,input);
  54.         }
  55.       }
  56.       delete []c;
  57.     }
  58.      
  59.     void make_move(int a[],int k, void *input)
  60.     {
  61.            
  62.     }
  63.      
  64.     void unmake_move(int a[], int k, void *input)
  65.     {
  66.            
  67.     }
  68.      
  69.     void construct_candidates(int a[], int k, void *input, int c[], int *ncandidates, int MAXCANDIDATES)
  70.     {
  71.             for(int i = 0; i < MAXCANDIDATES; i++)
  72.             {
  73.                     c[i] = i;
  74.                     (*ncandidates)++;
  75.             }
  76.     }
  77.      
  78.     bool is_a_solution(int a[],int k, void *input)
  79.     {
  80.             unsigned i = k;
  81.             std::vector<std::vector<int> > *datavector = static_cast<std::vector<std::vector<int> >* >(input);
  82.             if(i == ((*datavector).size()-2))
  83.                     return true;
  84.             else
  85.                     return false;
  86.     }
  87.      
  88.     void process_solution(int a[], int k, void *input, bool *finished)
  89.     {
  90.             int sumweight = 0;
  91.             int sumvalue = 0;
  92.             std::vector<std::vector<int> > *datavector = static_cast<std::vector<std::vector<int> >* >(input);
  93.             for(int i=0; i<k; i++)
  94.             {
  95.                     int obo = i+1;
  96.                     sumweight += a[obo] * (*datavector)[i+1][0];
  97.                     sumvalue += a[obo] * (*datavector)[i+1][1];
  98.             }
  99.             unsigned j = k+1;
  100.             if(((sumweight <= (*datavector)[0][0]) && (sumvalue > (*datavector)[j][1])) || ((sumweight < (*datavector)[j][0]) && (sumvalue == (*datavector)[j][1])))
  101.             {
  102.                     (*datavector)[j][0] = sumweight;
  103.                     (*datavector)[j][1] = sumvalue;
  104.                     for(int z=1; z<k+1; z++)
  105.                     {
  106.                             (*datavector)[z][2] = a[z];
  107.                     }
  108.             }
  109.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement