void nqueens(int n, int j, char *a, int *solutions, int depth) { int i; int *csols; if (n == j) { /* good solution, count it */ *solutions = 1; mycount++; return; } *solutions = 0; csols = alloca(n*sizeof(int)); memset(csols,0,n*sizeof(int)); /* try each possible position for queen <j> */ for (i = 0; i < n; i++) { #pragma omp task untied { /* allocate a temporary array and copy <a> into it */ char * b = alloca((j + 1) * sizeof(char)); memcpy(b, a, j * sizeof(char)); b[j] = i; if (ok(j + 1, b)) nqueens(n, j + 1, b,&csols[i],depth); } } #pragma omp taskwait for ( i = 0; i < n; i++) *solutions += csols[i]; }
void find_queens (int size) { total_count=0; printf("%d-Queens: ", size); #pragma omp parallel { #pragma omp single { char *a; a = alloca(size * sizeof(char)); #ifdef _OPENMP double start = omp_get_wtime();; #else clock_t start = clock(); #endif nqueens(size, 0, a, &total_count,0); //printf("completed!\n"); #ifdef _OPENMP double time = omp_get_wtime() - start; printf("%f sec\n", time); #else double end = clock(); float time = (float)(end-start) / CLOCKS_PER_SEC; printf("%f sec\n", time); #endif } #pragma omp atomic total_count += mycount; } }
void nqueens(char *config, int n, int i) { char *new_config; int j; if (i==n) { count++; } /* try each possible position for queen <i> */ for (j=0; j<n; j++) { /* allocate a temporary array and copy the config into it */ new_config = malloc((i+1)*sizeof(char)); memcpy(new_config, config, i*sizeof(char)); if (safe(new_config, i, j)) { new_config[i] = j; nqueens(new_config, n, i+1); } free(new_config); } // sync return; }
int main(int argc, char **argv) { #ifdef WOOL argc = wool_init(argc, argv); #endif if(argc < 2) { #ifdef WOOL fprintf(stderr, "Usage: nqueens <woolopt> <n>\n"); #else fprintf(stderr, "Usage: nqueens <n>\n"); #endif return 1; } int n = atoi(argv[1]); char *a = (char *) malloc(n * sizeof(char)); #ifdef WOOL printf("nqueens(%dx%d) = %d\n", n, n, CALL(nqueens, n, 0, a)); wool_fini(); #else printf("nqueens(%dx%d) = %d\n", n, n, nqueens(n, 0, a)); #endif return 0; }
int main() { int i; for (i = 1; i <= 8; i++) nqueens(i); return 0; }
int totalNQueens(int n) { res = 0; int *A = new int[n]; nqueens(A, 0, n); delete A; return res; }
int main(int argc, char *argv[]) { int n = 13; char *a; int res; if (argc < 2) { fprintf (stderr, "Usage: %s [<cilk-options>] <n>\n", argv[0]); fprintf (stderr, "Use default board size, n = 13.\n"); } else { n = atoi (argv[1]); printf ("Running %s with n = %d.\n", argv[0], n); } a = (char *) alloca (n * sizeof (char)); res = 0; res = nqueens(n, 0, a); if (res == 0) { printf ("No solution found.\n"); } else { printf ("Total number of solutions : %d\n", res); } return 0; }
void nqueens(int i, char **currentBoard, int currentProfit, int numQ, int maxProfit[], int maxProfIndex) { register int j; int n = gm->n; char ** maxBoard = gm->maxBoard; if (i < n) { for (j = 0; j < n; j++) { if (canPlace(i, j, currentBoard)) { // Creating a new board register int x,y; char **newBoard; newBoard = (char**)G_MALLOC(n*sizeof(char*)); for (x = 0; x < n; x++) { newBoard[x] = (char*)G_MALLOC(n*sizeof(char*)); for (y = 0; y < n; y++) newBoard[x][y] = currentBoard[x][y]; } newBoard[i][j] = 1; int profitAdd = abs(i - j); nqueens(i + 1, newBoard, currentProfit + profitAdd, numQ + 1, maxProfit, maxProfIndex); } } } else { gm->total = gm->total + 1; if (currentProfit > maxProfit[maxProfIndex]) { maxProfit[maxProfIndex] = currentProfit; gm->global_max_profit = currentProfit; for (i = 0; i < n; i++) for (j = 0; j < n; j++) gm->maxBoard[i][j] = currentBoard[i][j]; } } }
void nqueens(int n, int j, char *a, int *solutions, int depth) { int *csols; int i; if (n == j) { /* good solution, count it */ *solutions = 1; return; } *solutions = 0; csols = (int *)malloc(n*sizeof(int)); memset(csols,0,n*sizeof(int)); /* try each possible position for queen <j> */ for (i = 0; i < n; i++) { #pragma omp task untied firstprivate(n, csols, i, j, a, depth, solutions) { /* allocate a temporary array and copy <a> into it */ char * b = (char *)malloc(n * sizeof(char)); memcpy(b, a, j * sizeof(char)); b[j] = (char) i; if (ok(j + 1, b)) nqueens(n, j + 1, b,&csols[i],depth); //FIXME: depth or depth+1 ??? } } #pragma omp taskwait for ( i = 0; i < n; i++) *solutions += csols[i]; free(csols); }
vector<vector<string> > solveNQueens(int n) { res.clear(); vector<int> A(n, -1); nqueens(A, 0, n); return res; }
int Ex51::totalNQueens(int n) { int res=0; int *A = new int[n]; nqueens(A, 0, n, res); delete A; return res; }
/* Main Function */ int main() { int n; printf("\n Entrer Le nombre de reines: "); scanf("%d",&n); nqueens(n); getch(); }
int main() { queens_bruteforce(); nqueens(N); printf("\nPress any key to continue..."); getchar(); return(0); }
int main() { int k=1,n; printf("Enter the number n for n X n chessboard\n"); scanf("%d",&n); nqueens(k,n); return 0; }
int main(void) { int param; while (1) { scanf("%d", ¶m); printf("%d\n", nqueens(param)); } return 0; }
static gpointer nqueens_for(unsigned int start, unsigned int end, void *data, gint thread_number) { unsigned int i; for (i = start; i <= end; i++) { nqueens(0); } return NULL; }
void find_queens (int size) { char *a; total_count=0; a = alloca(size * sizeof(char)); bots_message("Computing N-Queens algorithm (n=%d) ", size); nqueens(size, 0, a, &total_count); bots_message(" completed!\n"); }
void Ex51::nqueens(int A[], int cur, int n, int &res) { if (cur == n) { res++; return; } else for (int i = 0; i < n; i++) { A[cur] = i; if (isValid(A, cur)) nqueens(A, cur+1, n, res); } }
int main() { printf("Enter the value of n:"); scanf("%d",&n); int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++) a[i][j]='.'; nqueens(0); return 0; }
void nqueens(vector<int> A, int cur, int n) { if (cur == n) printres(A, n); else { for (int i = 0; i < n; i++) { A[cur] = i; if (isValid(A, cur)) nqueens(A, cur + 1, n); } } }
/* Main Function */ int main() { int n; printf("----------------------------------------------------\n"); printf("-------------Made by C codechamp--------------------\n"); printf("----------------------------------------------------\n\n"); printf("\t C PROGRAM OF N-QUEEN PROBLEM\n\n"); printf("\nEnter the no. of Queens : "); scanf("%d",&n); printf("\n\n\tUSING %d QUEEN'S STRATEGY \n\n",n); nqueens(n); system("pause"); getch(); }
void nqueens(int A[], int cur, int n){ if (cur == n){ ++res; return; } else{ for (int i = 0; i < n; ++i){ A[cur] = i; if(isValid(A, cur)){ nqueens(A, cur + 1, n); } } } }
static void main_entrypoint(void *____arg) { main_entrypoint_ctx *ctx = (main_entrypoint_ctx *)____arg; int size; size = ctx->size; { total_count=0; bots_message("Computing N-Queens algorithm (n=%d) ", size); hclib_start_finish(); { char *a; a = (char *)malloc(size * sizeof(char)); nqueens(size, 0, a, &total_count,0); } ; hclib_end_finish(); bots_message(" completed!\n"); } ; free(____arg); }
int nqueens(int y) { int x; for (x = 0; x < QUEENS; x++) { if (safe((row[y - 1] = x), y - 1)) { if (y < QUEENS) { nqueens(y + 1); } else { break; } } } return 0; }
TASK_3(int, nqueens, int, n , int, j, char*, a) { #else int nqueens(int n, int j, char *a) { #endif // No more queens to place. if(n == j) { return 1; } // Try each possible position for queen <j> int solutions = 0; for(int i = 0; i < n; i++) { #ifdef WOOL SPAWN(nqueens_next, n, j, a, i); #else a[j] = i; if(ok(j + 1, a)) { solutions += nqueens(n, j + 1, a); } #endif } #ifdef WOOL for(int i = 0; i < n; i++) { solutions += SYNC(nqueens_next); } #endif return solutions; } #ifdef WOOL TASK_IMPL_4(int, nqueens_next, int, n, int, j, char*, a, int, i) { char *b = (char *) alloca((j + 1) * sizeof(char)); memcpy(b, a, j * sizeof(char)); b[j] = i; if(ok(j + 1, b)) { return CALL(nqueens, n, j + 1, b); } else { return 0; } }
void find_queens (int size) { { total_count=0; bots_message("Computing N-Queens algorithm (n=%d) ", size); { { char *a; a = (char *)malloc(size * sizeof(char)); nqueens(size, 0, a, &total_count,0); } } bots_message(" completed!\n"); } }
int main(int argc, char *argv[]) { int num_queens, *positions, count; if(argc != 2) { printf("Usage: %s <number of queens (chess board order)>\n", argv[0]); exit(1); } num_queens = atoi(argv[1]); initscr(); cbreak(); keypad(stdscr, TRUE); positions = nqueens(num_queens); free(positions); endwin(); return 0; }
void nqueens(int k,int n) { int i; for(i=1;i<=n;i++) { if(place(k,i)) { x[k]=i; if(k==n) { printf("The solution is\n"); print(n); } else nqueens(k+1,n); } } }
void nqueens_wrapper(void){ int i, j, n, p, pid; char **initialBoard; initialBoard = gm->initialBoard; int maxProfit[1]; maxProfit[0] = 0; n = gm->n; p = gm->p; GET_PID(pid); for (i = pid; i < n; i += p) { // initialBoard[0][i] = 1; nqueens(0, initialBoard, 0, 0, maxProfit, 0); // initialBoard[0][i] = 0; } }
int get_result(size_t size,size_t points_size,point_t *points,unsigned int ***result) { size_t iter = 0,iter_ = 0; *result = nqueens(size); if(!*result){ return 0; } while((*result)[iter]){ if(result_filter(size,points_size,points,(*result)[iter])){ (*result)[iter_] = (*result)[iter]; iter_++; } else{ free((*result)[iter]); } iter++; } (*result)[iter_] = NULL; return 1; }