main() { int n; scanf("%d",&n); nqueen(1,n); printf("%d",c); }
/** * Recursively solve nqueens using a backtracking algorithm **/ bool nqueen(int row) { g.recurse++; if (g.replay) { draw(); usleep(g.sleep); } // if we have finished all rows, we have succeded if (row >= g.n) return true; for (int i = 0; i < g.n; i++) { g.grid[row][i] = QUEEN; if (valid(row, i) && nqueen(row+1)) return true; else g.grid[row][i] = EMPTY; } return false; }
int main(int argc, char *argv[]) { if (argc != 2) return 1; n = atoi(argv[1]); nqueen(0); printf("%d\n", count); return 0; }
int main() { int x[20],n; printf("\nEnter the number of queens (max 20) = "); scanf("%d",&n); nqueen(x,n,0); return 0; }
int main() { printf("entre the num of queens\n"); scanf("%d",&n); if(n<=3 && n>1) printf(" no solution\n"); nqueen(); return 0; }
int totalNQueens(int n) { row.assign(n,0); column.assign(n,0); diagonal.assign(2*n,0); antidiagonal.assign(2*n,0); nqueen(0,n-1); return res; }
void main() { int i,j,k,no; int a[8][8]; printf("\n Enter the no of queens: "); scanf("%d",&no); nqueen(1,no); getch(); }
void nqueen(int k,int n){ int i,j; for(i=1;i<=n;i++) if(place(k,i)){ x[k]=i; if(k==n) c++; else nqueen(k+1,n); } }
/** * Decorator for nqueen function also measures performance of nqueen() **/ bool solve() { struct rusage before, after; getrusage(RUSAGE_SELF, &before); bool result = nqueen(0); getrusage(RUSAGE_SELF, &after); g.t_cpu = calculate(&before, &after); return result; }
int main() { int i, j; printf("\nEnter the no. of queens:- "); scanf("%d", &n); for (i = 0; i < n; i++) for (j = 0; j < n; j++) a[i][j] = '.'; nqueen(0); return (0); }
int main() { int n,i,j,kas=1; while(~scanf("%d",&n)) { min=1000000; q[1]=n; for(i=2;i<=8;i++) scanf("%d",&n),q[i]=n; nqueen(1); printf("Case %d: %d\n",kas++,min); } return 0; }
void main() { int n; clrscr(); cout<<"\n Enter no of Queens:"; cin>>n; cout<<"\nSolution of "<<n<<" Queen Problem"<<endl<<endl; nqueen(1,n); cout<<" \nTotal number of Solutions="<<count; for (int i=1;i<=n;i++) if (x[i]==0) cout<<"\nQueens can not be placed without being attacked ! Hence no solution"; getch(); }
void nqueen(int row) { int i, j; if (row < n) { for (i = 0; i < n; i++) { if (feasible(row, i)) { a[row][i] = 'Q'; nqueen(row + 1); a[row][i] = '.'; } } } else { printf("\nThe solution is:- "); printmatrix(); } }
void nqueen(int left, int right){ if(left>right){ res++; } for(int i=0;i<=right;i++){ if(!(column[i] ==0&&diagonal[left+i]==0&&antidiagonal[right+1+left-i]==0))continue; row[left]=i; column[i] =diagonal[left+i]=antidiagonal[right+1+left-i]=1; nqueen(left+1,right); column[i] =diagonal[left+i]=antidiagonal[right+1+left-i]=0; row[left]=0; } }
int main() { int re[11],k; for(int i=1;i<11;i++) { n=i; tot=0; nqueen(0); re[i]=tot; } while(scanf("%d",&k)!=EOF && k) { printf("%d\n",re[k]); } return 0; }
void nqueen(int k,int n) { /* k contains the current row under consideration. k also denotes the kth Queen being placed. */ for (int i=1;i<=n;i++) { /* This loop checks if a Queen can be placed in any of the possible cells in the kth row. Its tries all the cells in the row from first cell to nth cell */ if (place(k,i)) { x[k] = i; //place the queen if (k==n) { count++; for (int j=1;j<=n;j++) { for (int k=1;k<x[j];k++) cout<<"X "; cout<<"Q " ; for( k=x[j]+1;k<=n;k++) cout<<"X "; cout<<endl<<endl; } cout<<endl<<endl; } else nqueen(k+1,n); } else { //Nothing /* Rest of the branch rejected and new branch with next value of cell ie 'i' considered */ } } }
void nqueen(int x[],int n, int r) { int c; for(c=0;c<n;c++) { if(place(x,r,c)==1) { x[r]=c; if(r == (n-1)) { display(x,n); } else { nqueen(x,n,r+1); } } } }
void nqueen(int row) { for (int col = 0; col < n; col++) { if (cols[col]) continue; if (diag[col+row]) continue; if (diag2[col-row+n-1]) continue; // Check before entering the next depth. This is faster. if (row + 1 == n) { count++; return; } cols[col] = diag[col+row] = diag2[col-row+n-1] = true; nqueen(row + 1); cols[col] = diag[col+row] = diag2[col-row+n-1] = false; } }
void nqueen(int k) { int j,i,sum; for(j=1;j<=8;j++) { if(place(k,j)) { x[k]=j; if(k==8) { sum=0; for(i=1;i<=8;i++) sum+=((q[i]==x[i])?0:1); if(min>sum) min=sum; } else nqueen(k+1); } } }
void nqueen(int count) { int i,j; if(count==n) tot++; else { for(i=0;i<n;i++) { int ok=1; c[count]=i; for(j=0;j<count;j++) { if(c[count]==c[j]||abs(c[j]-c[count])==abs(j-count)) { ok=0; break; } } if(ok) nqueen(count+1); } } }
void nqueen(int k,int n) { int i; for(i=1; i<=n; i++) { if(place(k,i)==1) { x[k]=i; if(k==n) { printf("\n\n"); printf("\nQUEEN\tCOLUMN\n"); for(i=1; i<=n; i++) { printf("\n %d\t%d",i,x[i]); } } else { nqueen(k+1,n); } } } }
int main(int argc, char** argv) { ocd_init(&argc, &argv, NULL); ocd_initCL(); std::cerr << "N-Queen solver for OpenCL\n"; std::cerr << "Ping-Che Chen\n\n"; if(argc < 2) { std::cerr << "Usage: " << argv[0] << " [options] N\n"; std::cerr << "\tN: board size (1 ~ 32)\n"; std::cerr << "\t-cpu: use CPU (multi-threaded on Windows)\n"; std::cerr << "\t-prof: enable profiler\n"; std::cerr << "\t-threads #: set number of threads to #\n"; std::cerr << "\t-blocksize #: set size of thread blocks to #\n"; std::cerr << "\t-local: use local memory for arrays (default: off)\n"; std::cerr << "\t-noatomics: do not use global atomics\n"; std::cerr << "\t-novec: do not use vectorization\n"; std::cerr << "\t-vec4: use 4D vectors instead of 2D (only when vectorized- default: off)\n"; return 0; } // handle options bool force_cpu = false; bool profiling = false; int threads = 0; int block_size = 0; bool local = false;//default OFF (was true) bool noatomics = false; bool novec = false; bool use_vec4 = false; int start = 1; while(start < argc - 1) { if(std::strcmp(argv[start], "-cpu") == 0) { force_cpu = true; } else if(std::strcmp(argv[start], "-threads") == 0 && start < argc - 2) { threads = std::atoi(argv[start + 1]); start++; } else if(std::strcmp(argv[start], "-blocksize") == 0 && start < argc - 2) { block_size = std::atoi(argv[start + 1]); start++; } else if(std::strcmp(argv[start], "-local") == 0) { local = true; } else if(std::strcmp(argv[start], "-noatomics") == 0) { noatomics = true; } else if(std::strcmp(argv[start], "-novec") == 0) { novec = true; } else if(std::strcmp(argv[start], "-vec4") == 0) { use_vec4 = true; } else { std::cerr << "Unknown option " << argv[start] << "\n"; } start ++; } int board_size = std::atoi(argv[start]); if(board_size < 1 || board_size > 32) { std::cerr << "Inalid board size (only 1 ~ 32 allowed)\n"; return 0; } stopwatch sw; long long solutions = 0; long long unique_solutions = 0; if(force_cpu) { stopwatch_start(&sw); solutions = nqueen_cpu(board_size, &unique_solutions); stopwatch_stop(&sw); } else { stopwatch_start(&sw); cl_int err; // show device list size_t num_devices; num_devices=1;//In OpenDwarfs we only work with one device at a time. std::vector<cl_device_id> devices(num_devices / sizeof(cl_device_id)); devices.clear(); devices.resize(1); devices[0] = device_id; try { NQueenSolver nqueen(context, devices, profiling, threads, block_size, local, noatomics, novec, use_vec4); for(int i = 0; i < devices.size(); i++) { size_t name_length; err = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 0, 0, &name_length); if(err == CL_SUCCESS) { std::string name; name.resize(name_length + 1); clGetDeviceInfo(devices[i], CL_DEVICE_NAME, name_length, &name[0], &name_length); name[name_length] = 0; std::cerr << "Device " << i << ": " << name.c_str() << "\n"; std::cerr << "\tUsing " << nqueen.GetThreads(i) << " threads\n"; std::cerr << "\tBlock size = " << nqueen.GetBlockSize(i) << " threads\n"; if(nqueen.AtomicsEnabled(i)) { std::cerr << "\tUsing global atomics\n"; } if(nqueen.VectorizationEnabled(i)) { std::cerr << "\tUsing vectorization\n"; if(use_vec4) { std::cerr << "\tUse 4D vectors\n"; } else { std::cerr << "\tUse 2D vectors\n"; } } } } //start_time = std::clock(); solutions = nqueen.Compute(board_size, &unique_solutions); //end_time = std::clock(); } catch(CLError x) { if(x.GetErrorNo() == 1) { std::cerr << "1 OpenCL kernel execution failed\n"; } if(x.GetErrorNo() == 2) { std::cerr << "2 OpenCL kernel execution failed\n"; } if(x.GetErrorNo() == 3) { std::cerr << "3 OpenCL kernel execution failed\n"; } else { std::cerr << x << "\n"; } } stopwatch_stop(&sw); clReleaseContext(context); } std::cerr << "Solution took " << get_interval_by_sec(&sw) << " seconds to complete\n"; std::cerr << board_size << "-queen has " << solutions << " solutions (" << unique_solutions << " unique)\n"; printf("{ \"status\": %d, \"options\": \"-s %d\", \"time\": %f }\n", 1, board_size, get_interval_by_sec(&sw)); ocd_finalize(); return 0; }