예제 #1
0
void findSubset(int x0, int index, int numCount, int limit, int tab[20]) {

	int i, j;

	if (index - x0 + 1 == numCount) {

		if (numCount == 1) {

			for (i = 0; i < limit; i++) {
				printf("%d\n", tab[i]);
			}
		}
		else {

			for (j = index; j < limit; j++) {
				for (i = x0; i < index; i++) {

					printf("%d ", tab[i]);
				}
				printf("%d\n", tab[j]);
			}

			if (x0 != limit - numCount) {
				findSubset(x0 + 1, x0 + 1, numCount, limit, tab);
			}
		}
	}

	else {
		findSubset(x0, index + 1, numCount, limit, tab);
	}
}
예제 #2
0
 vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     vector<vector<int> > ret;
     vector<int>::iterator it;
     sort(candidates.begin(), candidates.end());
     it = unique(candidates.begin(), candidates.end());
     candidates.resize(it - candidates.begin());
     vector<int> comb;
     findSubset(candidates.begin(), candidates.end(), comb, target, ret);
     return ret;
 }
예제 #3
0
 void findSubset(vector<int>::iterator first, vector<int>::iterator last, vector<int> &comb, int target, vector<vector<int> > &ret)
 {
     if(target == 0)
     {
         ret.push_back(comb);
     }
     if(target < 0)
         return;
     vector<int>::iterator it;
     for(it = first; it != last && *it <= target; it++)
     {
         comb.push_back(*it);
         findSubset(it, last, comb, target - *it, ret);
         comb.pop_back();
     }
 }
예제 #4
0
int main() {

	int t, limit, numCount;
	int tab[20];

	scanf("%d", &t);

	for (int cond = 0; cond < t; cond++) {

		scanf("%d %d", &limit, &numCount);

		for (int i = 0; i < limit; i++) {
			tab[i] = i + 1;
		}

		findSubset(0, 0, numCount, limit, tab);

	}
	system("pause");
	return 0;
}
예제 #5
0
파일: SuDoku.C 프로젝트: whsalex/Sudoku
//Main function
int main()
{
    timeval newTime;
    int tmp_return         = -1;
    int ret                = 0;
    unsigned long runTime  = 0;
    float second           = 0;
    List* node             = NULL;
    List* result           = NULL;
   
    memset (&newTime,0x00,sizeof(newTime)); 
    //Initialize puzzle
    node = readNumber();
    if (NULL == node){
        printf ("Exit to input.\n");
        return SUDOKU_NG;
    }
    
    //Change form to binary
    tmp_return = doBinChange (&(node->block));
    if ( SUDOKU_NG == tmp_return){
        printf ("Error in binary change.\n");
        return SUDOKU_NG;
    }
    
    //Find the minimum subset
    while (1) {

        if ( 0 == node->block.number ){
            printf ("Error to find result.\n");
            return SUDOKU_NG;
        }
        
        tmp_return = findSubset(&(node->block));
        
        if ( SUDOKU_NG == tmp_return){
            printf ("Error in findSubset change.\n");
            return SUDOKU_NG;
            
        } else if ( SUDOKU_OK == tmp_return ){
            // Finish calculate, break
            break;
            
        } else if ( SUDOKU_DE == tmp_return ){
            //Make decision
            result = makeDecision (node);
            
            if ( NULL == result){
                printf ("Error in decision.\n");
                return SUDOKU_NG;
            }
   
            node = result;
            result = NULL;
            
        } else if ( SUDOKU_RE == tmp_return ){
            //Regress
            result = regress(node);
            
            if ( NULL == result){
                printf ("Error in regress.\n");
                return SUDOKU_NG;
            }

            node = result;
            result = NULL;
            
        } else {
            printf ("Error in findSubset change.\n");
            return SUDOKU_NG;
        }
    }
   
    //change form to decimal 
    doDecChange(&(node->block));

    ret = gettimeofday( &newTime, NULL );
    
    if (-1 == ret){
        printf ("Call gettimeofday failed.\n");
    } else {
/*
#if defined DEBUG
        printf ("Finished!Now sec is %u,Usec is %u.\n",
               newTime.tv_sec,newTime.tv_usec);
#endif
*/
        runTime = (newTime.tv_sec - currentTime.tv_sec)*1000000 +
                   newTime.tv_usec - currentTime.tv_usec;
        second = runTime/1000000.0;
        printf ("Run time is %f second.\n",second);       
    }

    //print result
    printBlock(&(node->block));

    return SUDOKU_OK;
}