コード例 #1
0
ファイル: generate_subset.cpp プロジェクト: ahmed-sharif/misc
void backtrack(int a[], int k, int input, int indent)
{
    int c[20]; /* candidates for next position */
    int ncandidates; /* next position candidate count */
    int i; /* counter */
    for (i=0; i<indent; i++)
    {
        printf("\t");
    }
    for(i=1; i<=k;i++)
        printf("%d ", a[i]);

    printf("k=%d, indent=%d\n",k, indent);

    if (is_a_solution(a,k,input))
    {
        process_solution(a,k, indent);
    }
    else 
    {
        k = k+1;
        construct_candidates(a,k,input,c,&ncandidates);
        for (i=0; i<ncandidates; i++) 
        {
            a[k] = c[i];
            backtrack(a,k,input, indent+1);
            if (finished) 
                return; /* terminate early */
        }
    }
}
コード例 #2
0
void backtrack(int a[], int k, void* input)
{
    int c[MAXCANDIDATES];
    int ncandidates;

    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];

            make_move(a, k, input);
            backtrack(a, k, input);
            unmake_move(a, k, input);

            if (finished)
                return;
        }
    }
}
コード例 #3
0
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);
        }
    }

}
コード例 #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);
                }
        }
}
コード例 #5
0
ファイル: 140.c プロジェクト: 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 */
		}
	}
}
コード例 #6
0
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 */
        }
    }
}
コード例 #7
0
ファイル: SOS.CPP プロジェクト: raihanmasud/acm-icpc
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; */
		}
	}
}
コード例 #8
0
ファイル: 10576.c プロジェクト: bhrzslm/practice
void backtrack(int* partial, int position, long int gain, long int gain5) {

  if (position== 11) {
    if (gain > best)
    best = gain;
  }
  else {
    int nCandidates;
    int candidates[2];
    int counter;
    long int oldGain, oldGain5;
    /*construct candidates for the next position*/
    construct_candidates (partial, position+1, candidates, &nCandidates);
    /* printf("number of candidates:%d\n" , nCandidates); */
    for (counter = 0; counter < nCandidates; counter++) {
      oldGain5 = gain5;
      oldGain = gain;
      if (candidates[counter])
	gain += s;
      else
	gain -= d;
      if (position + 1 == 4) 
	gain5 = gain;
      if (position + 1 > 4) {
	if (partial[position-4] == 1)
	  gain5 -= s;
	else
	  gain5 += d;

	if (candidates[counter]) 
	  gain5 += s;
	else 
	  gain5 -= d;
      }
      if (position + 1 < 4 ||
	  gain5 < 0) {
	partial [position+1] = candidates[counter];
	backtrack (partial, position + 1, gain, gain5);
      }
      gain = oldGain;
    gain5 = oldGain5;
    }
  }
}
コード例 #9
0
ファイル: gen_subsets.c プロジェクト: walrus7521/code
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;
        }
    }
}
コード例 #10
0
ファイル: nqueens.c プロジェクト: walrus7521/code
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;
        }
    }
}
コード例 #11
0
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;
    }
  }
} 
コード例 #12
0
ファイル: 10503.c プロジェクト: bhrzslm/practice
void backtrack(int tail, int position) {
  if (position == n-1)
    found = 1;
  else if (!found) {
    int nCandidates;
    int candidates[14];
    int sides[14];
    construct_candidates (tail, position + 1, candidates, sides, &nCandidates);
    int counter;
    for (counter = 0; !found && counter < nCandidates; counter++) {
      int nextTail;
      if (sides[counter])
	nextTail = tails[candidates[counter]];
      else
	nextTail = heads[candidates[counter]];
      used[candidates[counter]] = 1;
      backtrack (nextTail, position + 1);
      used[candidates[counter]] = 0;
    }
  }
}
コード例 #13
0
ファイル: main.c プロジェクト: roblapp/uva_online_judge
bool backtrack( int k )
{
    int i, j, ncandidates;
    int push;
    int len = strlen( input[k] );
    int c[MAXWORDS];
    char temp[ALPHA];
    bool failed;

    if ( k == niw )
    {
        return TRUE;
    }

    construct_candidates( k, c, &ncandidates, len );

    for ( i = 0; i < ncandidates; i++ ) /* for each candidate */
    {
        failed = FALSE;
        push = 0;

        for ( j = 0; j < len && !failed; j++ )
        {
            /* c[i] contains an index of the dictionary */
            /* we want to change input[k][j] to dictionary[ c[i] ][j] */
            if ( map[ input[k][j] - OFFSET ] == -1 && parent[ dictionary[ c[i] ][j] - OFFSET  ] == -1 ) /* not yet mapped */
            {
                map[ input[k][j] - OFFSET ] = dictionary[ c[i] ][j];
                parent[ dictionary[ c[i] ][j] - OFFSET ] = input[k][j];
                temp[push++] = input[k][j];
                temp[push++] = dictionary[ c[i] ][j];
            }
            else if ( map[ input[k][j] - OFFSET ] == dictionary[ c[i] ][j]
                     &&
                     parent[ dictionary[ c[i] ][j] - OFFSET ] == input[k][j] )
            {
                /* already been mapped, but to the "correct" character */
                continue;
            }
            else /* there is a failure */
            {
                failed = TRUE;
            }
        }

        if ( !failed && backtrack( k + 1 ) )
        {
            return TRUE;
        }
        else
        {
            for ( j = 0; j < push; j += 2 )
            {
                map[ temp[j] - OFFSET ] = -1;
                parent[ temp[j + 1] - OFFSET ] = -1;
            }
        }
    }

    return FALSE;
}