Beispiel #1
0
void backtrack(int dms)
{
    int ncandidate, i, row;
    int candidates[N];
    if (dms == size) ++total;
    else {
        construct_solutions(dms, candidates, &ncandidate);
        for (i = 0; i < ncandidate; ++i) {
            row = candidates[i];
            solutions[dms] = row;
            used[row] = 1;
            backtrack(dms + 1);
            used[row] = 0;
        }
    }
}
Beispiel #2
0
Datei: 725.c Projekt: treblih/oj
void backtrack(int dms)
{
	int i, ncandidate, tmp;
	int candidates[10];
	if (dms == 5) process_solutions();
	else {
		construct_solutions(dms, candidates, &ncandidate);
		for (i = 0; i < ncandidate; ++i) {
			tmp = candidates[i];
			/* solutions[] holding the ascii char */
			solutions[dms] = tmp + 0x30;
			used[tmp] = true;
			backtrack(dms + 1);
			used[tmp] = false;
		}
	}
}
Beispiel #3
0
gcp_solution_t* antcol(gcp_t *problem) {/*{{{*/

	int c = 0;
	gcp_solution_t *result;
	result = malloc(sizeof(gcp_solution_t));

	initialize_data(problem);
	best_ant->ncolors = INT_MAX;

	int terminate = FALSE;

	while (!terminate) {

		initialize_var_phero(problem);
		construct_solutions(problem, c);
		local_search(problem);
		update_pheromone_trails(problem);

		c++;

		if (problem->flags & FLAG_VERBOSE) {
			printf("Cycle %d - Colors used: %d\n", c, best_ant->ncolors);
		}
		else {
			printf("*");
			fflush(stdout);
		}

		/* test stop conditions */
		if ((problem->flags & FLAG_CYCLE) && problem->ncycles <= c)
			terminate = TRUE;
		if ((problem->flags & FLAG_TIME) && problem->time <= current_usertime_secs())
			terminate = TRUE;


	}

	*result = *best_ant;
	result->ncycles = c;

	return result;

}/*}}}*/
Beispiel #4
0
Datei: 729.c Projekt: treblih/oj
void backtrack(int dms)
{
	int i, ncandidate, n = 1;
	int candidates[2];
	candidates[0] = '0'; 
	if (h) {candidates[1] = '1'; n = 2;}

	/* h == 0 */
	if (dms == bit) puts(solutions);
	else if (dms + h = bit) candidates[0] = '1';
	else {
		construct_solutions(dms, candidates, &ncandidate);
		for (i = 0; i < n; ++i) {
			solutions[dms] = candidates[i];
			if (i) --h;
			backtrack(dms + 1);
			if (i) ++h;
		}
	}
}
Beispiel #5
0
void backtrack(int dms)
{
	int ncandidate, i, row;
	int candidates[N];
	if (dms == N && min > total) {
		min = total;
	}
	else {
		construct_solutions(dms, candidates, &ncandidate);
		for (i = 0; i < ncandidate; ++i) {
			row = candidates[i];
			/* pruning 
			 * 0-7 vs. 1-8 */
			if (row != arr[dms] - 1) ++total;
			if (total == min) {--total; continue;}
			solutions[dms] = row;
			used[row] = 1;
			backtrack(dms + 1);
			used[row] = 0;
			/* 0-7 vs. 1-8 */
			if (row != arr[dms] - 1) --total;
		}
	}
}
Beispiel #6
0
Datei: 639.c Projekt: treblih/oj
void backtrack(int y, int x)
{
	int i, ncandidate, tmp;
	int candidates[2];
	/* (1, len), 1 unit exceed */
	if (y == len && !x) process_solutions();
	else if (graph[y][x] == 'X') {
		used_y[y] = false;
		used_x[x] = false;
		if (x == len - 1) backtrack(y + 1, 0);
		else backtrack(y, x + 1);
	} else {
		construct_solutions(y, x, candidates, &ncandidate);
		for (i = 0; i < ncandidate; ++i) {
			tmp = candidates[i];
			graph[y][x] = tmp;
			/* rook */
			if (tmp == '*') used_y[y] = used_x[x] = true;
			if (x == len - 1) backtrack(y + 1, 0);
			else backtrack(y, x + 1);
			if (tmp == '*') used_y[y] = used_x[x] = false;
		}
	}
}
Beispiel #7
0
int main(int argc, char *argv[]) {/*{{{*/


	extern char *optarg;
	char op, *namefilein;
	int it;

	ALPHA = 2;
	BETA = 1;
	RHO = 0.4;
	M = 40;
	it = 1000;


	/* Usando getopt para tratamento dos argumentos */
	struct option longopts[] = {/*{{{*/
		{"alpha", 1, NULL, 'a'},	
		{"beta", 1, NULL, 'b'},	
		{"rho", 1, NULL, 'r'},	
		{"ants", 1, NULL, 'm'},
		{"iterations", 1, NULL, 'i'},
		{"help", 0, NULL, 'h'},
	};/*}}}*/


	while ((op = getopt_long(argc, argv, "a:b:r:m:i:h", longopts, NULL)) != -1) {/*{{{*/
		switch (op) {
			case 'a':
				ALPHA = atoi(optarg);
				break;
			case 'b':
				BETA = atoi(optarg);
				break;
			case 'r':
				RHO = atoi(optarg);
				break;
			case 'm':
				M = atoi(optarg);
				break;
			case 'i':
				it = atoi(optarg);
				break;
			case 'h':
				print_help(argv[0]);
				return 0;
//			case ':':
//				/* a opção anterior precisa de especificação mas não recebeu */
//				printf("Erro: arquivo de saida nao especificado. Use '-h' para ajuda.\n");
//				return 0;
//			case '?':
//				/* opção não existe */
//				printf("Erro: opcao '-%c' nao existente. Use '-h' para ajuda.\n", optopt);
//				return 0;
		}
	}/*}}}*/


/*{{{*/
	/* O único argumento não capturado acima é o nome do arquivo de entrada,
	 * se existir */
	if (optind < argc) {
		namefilein = malloc(sizeof(char) * strlen(argv[optind]));
		strcpy(namefilein, argv[optind++]);

		/* verificar se foi passado algum argumento a mais */
		if (optind < argc) {
			printf("Erro: foram passados muitos argumentos. Use '-h' para ajuda.\n");
			return 0;
		}
	}
	else {
		printf("Erro: falta nome do arquivo de entrada. Use '-h' para ajuda.\n");
		return 0;
	}


	if (!fopen(namefilein, "r")) {
		printf("Erro: arquivo ou diretorio inexistente.\n");
		return 0;
	}
/*}}}*/


	int terminate = 0;
	int k = 0;

	initialize_data(namefilein);

	while (!terminate && k != it) {
		construct_solutions();
		local_search();
		//update_statistics();
		update_pheromone_trails();
		k++;
	}


	int menor = 0;
	for (k = 1; k < M; k++) {
		if (ant[menor].tour_length > ant[k].tour_length) {
			menor = k;
		}
	}
	printf("%d: %d\n", menor, ant[menor].tour_length);

	return 0;
}/*}}}*/