void backtrack(vector<int>& nums,int k, int n) {
    if (is_a_solution(nums, k ,n ))
        process_solution(nums);
    else {
        vector<int> cand;
        construct_cand(nums, k, n, cand);
        for (int i = 0; i < cand.size(); i++) {
            nums.push_back(cand.at(i));
            backtrack(nums, k, n);
            nums.pop_back();
        }
    }
}
void backtrack(int a[], int k, int input){
    int candidates[MAX+1];
    int num_candidates = 0;
    if(correct_solution(a, k, input))
        process_solution(a, input);
    else {
        k++;
        construct_candidates(a, k, input, candidates, &num_candidates);
        int i = 0;
        for(; i < num_candidates; i++){
            a[k-1] = candidates[i];
            backtrack(a, k, input);
        }
    }

}
void backtrack(int a[], int k, int input) {
    int c[MAXCANDIDATES];   /* candidates for next position */
    int ncandidates;        /* next position candidate count */

    if (is_a_solution(a,k,input))
        process_solution(a,k,input);
    else {
        k = k+1;
        construct_candidates(a,k,input,c,ncandidates);

        for(int i = 0; i < ncandidates; i++) {
            a[k] = c[i];
            backtrack(a,k,input);
            if (finished) return;   /* terminate early */
        }
    }
}
Example #4
0
void backtrack(int a[], int k, int n,char items[])
{
        int c[MAXCANDIDATES];           /* candidates for next position */
        int ncandidates;                /* next position candidate count */
        int i;                          /* counter */

        if (is_a_solution(a, k, n))
                 process_solution(a, k, items);
        else {
                k = k+1;
                construct_candidates(a, k, n, c, &ncandidates);
                for (i=0; i<ncandidates; i++) {
                        a[k] = c[i];
                        backtrack(a, k, n,items);
                }
        }
}
Example #5
0
File: 140.c Project: treblih/oj
void backtrack(int dms)
{
	int ncandidate, i, tmp;
	int candidates[SIZE];
	if (dms == cnt) process_solution();
	else {
		construct_candidates(dms, candidates, &ncandidate);
		for (i = 0; i < ncandidate; ++i) {
			tmp = candidates[i];
			solution[dms] = tmp;
			used[tmp] = 2;	/* set */
			backtrack(dms + 1);
			if (finished) return ;
			used[tmp] = 1;	/* unset */
		}
	}
}
Example #6
0
void backtrack(int a[], int k, int n)
{
	int c[N];
	int candidates;
	if(check_solution(a,k,n))
		process_solution(a,k);

	else{
		k=k+1;
		construct_candidates(a,k,c,&candidates);
		for(int i=0;i<candidates;i++){
			a[k]=c[i];
			backtrack(a,k,n);
/*			if(FALSE)
				return; */
		}
	}
}
Example #7
0
static void backtrack(int a[], int k, int n)
{
    int c[CANDIDATES_MAX + 1];
    int nr_candidates;
    int i;

    if (is_a_solution(a, k, n))
        process_solution(a, k);
    else {
        k += 1;
        generate_candidates(a, k, n, c, &nr_candidates);
        for (i = 0; i < nr_candidates; i++) {
            a[k] = c[i];
            backtrack(a, k, n);
            if (finished)
                return;
        }
    }
}
Example #8
0
void backtrack(int a[], int k, int input)
{
    int c[MAXCANDIDATES], ncandidates, i;
    dprintf("backtrack(k=%d, in=%d)...a[", k, input);
    for (int i = 1; i <= k; ++i) dprintf("%d, ", a[i]);
    dprintf("]\n");
    if (is_a_solution(a, k, input))
        process_solution(a, k);
    else {
        k++;
        construct_candidates(a, k, input, c, &ncandidates);
        dprintf("process %d candidates\n", ncandidates);
        for (i = 0; i < ncandidates; i++) {
            a[k] = c[i];
            backtrack(a, k, input);
            dprintf("after call to backtrack for k = %d, i = %d\n", k, i);
            if (finished) return;
        }
    }
}
Example #9
0
void backtrack(int a[], int k, int input)
{
    int c[MAXCANDIDATES];
    int ncandidates;
    int i;

    dbg_log(1, "backtrack - enter k %d\n", k);
    if (is_a_solution(a, k, input))
        process_solution(a, k);
    else {
        k++;
        construct_candidates(a, k, input, c, &ncandidates);
        dbg_log(1, "process %d candidates\n", ncandidates);
        for (i = 0; i < ncandidates; i++) {
            a[k] = c[i];
            backtrack(a, k, input);
            if (finished) return;
        }
    }
}
void backtrack(int a[], int k, data input)
{
  int c[MAXCANDIDATES];         /* candidates for next position */
  int ncandidates;              /* next position candidates count */
  int i;


  if (is_a_solution(a, k, input))
    process_solution(a, k, input);
  else {
    ++k;
    construct_candidates(a, k, input, c, &ncandidates);
    for (i=0; i<ncandidates; ++i) {
      a[k] = c[i];
      make_move(a, k, input);
      backtrack(a, k, input);
      unmake_move(a, k, input);
      if (finished) return;
    }
  }
} 
Example #11
0
 void generate_subsequences(const std::string& from
         , int index
         , std::string& to
         , std::unordered_set<std::string>& subsequences)
 {
     if(be_a_solution(from, index))
     {
         process_solution(to, subsequences);
     }
     else
     {
         //construct_candidates
         char candidates[2] = {from[index], '\0'};
         int sz=to.size();
         for(size_t i=0; i<2; ++i)
         {
             if(candidates[i]!='\0')
                 to.push_back(candidates[i]);
             //make_move
             generate_subsequences(from, index+1, to, subsequences);
             unmake_move(to, sz);
         }
     }
 }
Example #12
0
inline void EnumerationBase::enumerate_recursive(
    EnumerationBase::opts<kk, kk_start, dualenum, findsubsols, enable_reset>)
{
  enumf alphak  = x[kk] - center[kk];
  enumf newdist = partdist[kk] + alphak * alphak * rdiag[kk];

  if (!(newdist <= partdistbounds[kk]))
    return;
  ++nodes;

  alpha[kk] = alphak;
  if (findsubsols && newdist < subsoldists[kk])
  {
    subsoldists[kk] = newdist;
    process_subsolution(kk, newdist);
  }

  if (kk == 0)
  {
    if (newdist > 0.0 || !is_svp)
      process_solution(newdist);
  }
  else if (enable_reset &&
           kk < reset_depth)  // in CVP, below the max GS vector, we reset the partial distance
  {
    reset(newdist, kk);
    return;
  }
  else
  {
    partdist[kk - 1] = newdist;
    if (dualenum)
    {
      for (int j                   = center_partsum_begin[kk]; j > kk - 1; --j)
        center_partsums[kk - 1][j] = center_partsums[kk - 1][j + 1] - alpha[j] * mut[kk - 1][j];
    }
    else
    {
      for (int j                   = center_partsum_begin[kk]; j > kk - 1; --j)
        center_partsums[kk - 1][j] = center_partsums[kk - 1][j + 1] - x[j] * mut[kk - 1][j];
    }
    if (center_partsum_begin[kk] > center_partsum_begin[kk - 1])
      center_partsum_begin[kk - 1] = center_partsum_begin[kk];
    center_partsum_begin[kk]       = kk;
    center[kk - 1]                 = center_partsums[kk - 1][kk];
    roundto(x[kk - 1], center[kk - 1]);
    dx[kk - 1] = ddx[kk - 1] = (((int)(center[kk - 1] >= x[kk - 1]) & 1) << 1) - 1;
  }

  while (true)
  {
    FPLLL_TRACE("Level k=" << kk << " dist_k=" << partdist[kk] << " x_k=" << x[kk]
                           << " newdist=" << newdist << " partdistbounds_k=" << partdistbounds[kk]);
    enumerate_recursive(opts<kk - 1, kk_start, dualenum, findsubsols, enable_reset>());

    if (partdist[kk] != 0.0)
    {
      x[kk] += dx[kk];
      ddx[kk] = -ddx[kk];
      dx[kk]  = ddx[kk] - dx[kk];

      enumf alphak2  = x[kk] - center[kk];
      enumf newdist2 = partdist[kk] + alphak2 * alphak2 * rdiag[kk];
      if (!(newdist2 <= partdistbounds[kk]))
        return;
      ++nodes;
      alpha[kk] = alphak2;
      if (kk == 0)
      {
        if (newdist2 > 0.0 || !is_svp)
          process_solution(newdist2);
      }
      else
      {
        partdist[kk - 1] = newdist2;
        if (dualenum)
          center_partsums[kk - 1][kk - 1 + 1] =
              center_partsums[kk - 1][kk - 1 + 1 + 1] - alpha[kk - 1 + 1] * mut[kk - 1][kk - 1 + 1];
        else
          center_partsums[kk - 1][kk - 1 + 1] =
              center_partsums[kk - 1][kk - 1 + 1 + 1] - x[kk - 1 + 1] * mut[kk - 1][kk - 1 + 1];
        if (kk > center_partsum_begin[kk - 1])
          center_partsum_begin[kk - 1] = kk;
        center[kk - 1]                 = center_partsums[kk - 1][kk - 1 + 1];
        roundto(x[kk - 1], center[kk - 1]);
        dx[kk - 1] = ddx[kk - 1] = (((int)(center[kk - 1] >= x[kk - 1]) & 1) << 1) - 1;
      }
    }
    else
    {
      ++x[kk];

      enumf alphak2  = x[kk] - center[kk];
      enumf newdist2 = partdist[kk] + alphak2 * alphak2 * rdiag[kk];
      if (!(newdist2 <= partdistbounds[kk]))
        return;
      ++nodes;
      alpha[kk] = alphak2;
      if (kk == 0)
      {
        if (newdist2 > 0.0 || !is_svp)
          process_solution(newdist2);
      }
      else
      {
        partdist[kk - 1] = newdist2;
        if (dualenum)
          center_partsums[kk - 1][kk - 1 + 1] =
              center_partsums[kk - 1][kk - 1 + 1 + 1] - alpha[kk - 1 + 1] * mut[kk - 1][kk - 1 + 1];
        else
          center_partsums[kk - 1][kk - 1 + 1] =
              center_partsums[kk - 1][kk - 1 + 1 + 1] - x[kk - 1 + 1] * mut[kk - 1][kk - 1 + 1];
        if (kk > center_partsum_begin[kk - 1])
          center_partsum_begin[kk - 1] = kk;
        center[kk - 1]                 = center_partsums[kk - 1][kk - 1 + 1];
        roundto(x[kk - 1], center[kk - 1]);
        dx[kk - 1] = ddx[kk - 1] = (((int)(center[kk - 1] >= x[kk - 1]) & 1) << 1) - 1;
      }
    }
  }
}
Example #13
0
template <bool dualenum, bool findsubsols, bool enable_reset> void EnumerationBase::enumerate_loop()
{
  if (k >= k_end)
    return;

  for (int i = 0; i < k_end; ++i)
  {
    center_partsum_begin[i + 1] = k_end - 1;
    center_partsums[i][k_end]   = center_partsum[i];
  }

  partdist[k_end] = 0.0;  // needed to make next_pos_up() work properly

  nodes -= k_end - k;
  k = k_end - 1;

#ifdef FPLLL_WITH_RECURSIVE_ENUM
  enumerate_recursive_dispatch<dualenum, findsubsols, enable_reset>(k);
  return;
#endif

  finished = false;
  while (!finished)
  {
    enumf alphak  = x[k] - center[k];
    enumf newdist = partdist[k] + alphak * alphak * rdiag[k];
    FPLLL_TRACE("Level k=" << k << " dist_k=" << partdist[k] << " x_k=" << x[k]
                           << " newdist=" << newdist << " partdistbounds_k=" << partdistbounds[k]);
    if (newdist <= partdistbounds[k])
    {
      ++nodes;
      alpha[k] = alphak;
      if (findsubsols && newdist < subsoldists[k])
      {
        subsoldists[k] = newdist;
        process_subsolution(k, newdist);
      }
      --k;
      if (k < 0)
      {
        if (newdist > 0.0 || !is_svp)
          process_solution(newdist);
        finished = !next_pos_up();
        continue;
      }
      if (enable_reset &&
          k < reset_depth)  // in CVP, below the max GS vector, we reset the partial distance
      {
        reset(newdist, k);
        finished = !next_pos_up();
        continue;
      }
      if (dualenum)
      {
        for (int j              = center_partsum_begin[k + 1]; j > k; --j)
          center_partsums[k][j] = center_partsums[k][j + 1] - alpha[j] * mut[k][j];
      }
      else
      {
        for (int j              = center_partsum_begin[k + 1]; j > k; --j)
          center_partsums[k][j] = center_partsums[k][j + 1] - x[j] * mut[k][j];
      }
      center_partsum_begin[k]     = max(center_partsum_begin[k], center_partsum_begin[k + 1]);
      center_partsum_begin[k + 1] = k + 1;

      enumf newcenter = center_partsums[k][k + 1];
      center[k]       = newcenter;
      partdist[k]     = newdist;
      roundto(x[k], newcenter);
      dx[k] = ddx[k] = (((int)(newcenter >= x[k]) & 1) << 1) - 1;
    }
    else
    {
      finished = !next_pos_up();
    }
  }
}