ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1005. Stone Pile

Don't know why have wrong ans?
Posted by carrtopfea 23 Jun 2008 11:31
#include <stdio.h>
#include <stdlib.h>

bool finished = 0; /* found all solutions yet? */
int suma;
int mind = 99999;
int is_a_solution(int a[], int k, int n,int p[])
{
       return (k == n); /* is k == n? */
}

void  sum(int p[], int n){
   int i;
   int re = 0;
   for(i=1;i<=n;i++)
    {re+=p[i];
     //printf("re: %d\n",re);

    }
    suma = re;

}



void process_solution(int a[], int k, int input,int p[])
{      int sum1=0;
       int re = 0 ;
       int i; /* counter */
    //  printf("{");
       int count = 0;

       for(i=1;i<=k;i++) if (a[i] == 1) count ++;
       if(count == input) mind = 9999;
    else{
       for (i=1; i<=k; i++)
               if (a[i] == 1){
                        sum1 += p[i];
                   //    printf(" %d ", i);
                        }
                    //    printf("\n");
                        re = (suma -sum1)-sum1;

                         if(re<mind & re >= 0) mind =re;
       }
                      //    printf("diff: %d  mind: %d\n\n",re, mind);
                       // printf("%d\n",mind);

                       // printf("\n\n");


      // printf(" }\n");
}

void construct_candidates(int a[], int k, int n, int c[], int *ncandidates)
{
       c[0] = 1;
       c[1] = 0;
       *ncandidates = 2;
}

void backtrack(int a[], int k, int input, int p[])
{
       int c[100]; /* candidates for next position */
       int ncandidates; /* next position candidate count */
       int i; /* counter */
       if (is_a_solution(a,k,input,p))
       process_solution(a,k,input,p);
       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,p);
                       if (finished) return; /* terminate early */
               }
       }
}


int main()
{
         int a[25]={}, n , p[25];
        int i;
      while(1){
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        scanf("%d",&p[i]);

        sum(p,n);
            backtrack(a,0,n,p);

      printf("%d\n",mind);
      }



}