Data update
This commit is contained in:
parent
29a5eea0d4
commit
5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions
|
|
@ -1,31 +1,87 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int count = 0;
|
||||
void solve(int n, int col, int *hist)
|
||||
{
|
||||
if (col == n) {
|
||||
printf("\nNo. %d\n-----\n", ++count);
|
||||
for (int i = 0; i < n; i++, putchar('\n'))
|
||||
for (int j = 0; j < n; j++)
|
||||
putchar(j == hist[i] ? 'Q' : ((i + j) & 1) ? ' ' : '.');
|
||||
// In column order, print out the given positions in chess notation.
|
||||
// For example, when N = 8, the first solution printed is:
|
||||
// "a1 b5 c8 d6 e3 f7 g2 h4"
|
||||
static void print_positions(int x[], const size_t n) {
|
||||
static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
return;
|
||||
// There are only 26 letters in the ASCII alphabet, so
|
||||
// so don't bother with chess notation above 26.
|
||||
if (n <= 26) {
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
printf("%c%u ", alphabet[i], x[i] + 1);
|
||||
} else {
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
printf("%u ", x[i] + 1);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
# define attack(i, j) (hist[j] == i || abs(hist[j] - i) == col - j)
|
||||
for (int i = 0, j = 0; i < n; i++) {
|
||||
for (j = 0; j < col && !attack(i, j); j++);
|
||||
if (j < col) continue;
|
||||
// Print all solutions to the N queens problem, holding the results in
|
||||
// the intermediate array x, and with the auxiliary boolean arrays a, b, and c.
|
||||
// x and a are both N elements long, while b and c are 2*N-1 elements long.
|
||||
// It is assumed that these arrays are zeroed before this routine is called.
|
||||
static void queens(int x[], bool a[], bool b[], bool c[], const size_t n) {
|
||||
size_t col, row = 0;
|
||||
|
||||
hist[col] = i;
|
||||
solve(n, col + 1, hist);
|
||||
advance_row:
|
||||
if (row >= n) {
|
||||
print_positions(x, n);
|
||||
goto backtrack;
|
||||
}
|
||||
col = 0;
|
||||
try_column:
|
||||
if (!a[col] && !b[col+row-1] && !c[col-row+n]) {
|
||||
a[col] = true;
|
||||
b[col+row-1] = true;
|
||||
c[col-row+n] = true;
|
||||
x[row] = col;
|
||||
row++;
|
||||
goto advance_row;
|
||||
}
|
||||
try_again:
|
||||
if (col < n-1) {
|
||||
col++;
|
||||
goto try_column;
|
||||
}
|
||||
backtrack:
|
||||
if (row != 0) {
|
||||
--row;
|
||||
col = x[row];
|
||||
c[col-row+n] = false;
|
||||
b[col+row-1] = false;
|
||||
a[col] = false;
|
||||
goto try_again;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int n, char **argv)
|
||||
{
|
||||
if (n <= 1 || (n = atoi(argv[1])) <= 0) n = 8;
|
||||
int hist[n];
|
||||
solve(n, 0, hist);
|
||||
static void *calloc_wrapper(size_t count, size_t bytesize) {
|
||||
void *r;
|
||||
if ((r = calloc(count, bytesize)) == NULL) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
bool *a, *b, *c;
|
||||
int n, *x;
|
||||
|
||||
if (argc != 2 || (n = atoi(argv[1])) <= 0) {
|
||||
printf("%s: specify a natural number argument\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
x = calloc_wrapper(n, sizeof(x[0]));
|
||||
a = calloc_wrapper(n, sizeof(a[0]));
|
||||
b = calloc_wrapper((2 * n - 1), sizeof(b[0]));
|
||||
c = calloc_wrapper((2 * n - 1), sizeof(c[0]));
|
||||
|
||||
queens(x, a, b, c, n);
|
||||
|
||||
// Don't bother freeing before exiting.
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,38 +1,31 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef uint32_t uint;
|
||||
uint full, *qs, count = 0, nn;
|
||||
|
||||
void solve(uint d, uint c, uint l, uint r)
|
||||
int count = 0;
|
||||
void solve(int n, int col, int *hist)
|
||||
{
|
||||
uint b, a, *s;
|
||||
if (!d) {
|
||||
count++;
|
||||
#if 0
|
||||
printf("\nNo. %d\n===========\n", count);
|
||||
for (a = 0; a < nn; a++, putchar('\n'))
|
||||
for (b = 0; b < nn; b++, putchar(' '))
|
||||
putchar(" -QQ"[((b == qs[a])<<1)|((a + b)&1)]);
|
||||
#endif
|
||||
if (col == n) {
|
||||
printf("\nNo. %d\n-----\n", ++count);
|
||||
for (int i = 0; i < n; i++, putchar('\n'))
|
||||
for (int j = 0; j < n; j++)
|
||||
putchar(j == hist[i] ? 'Q' : ((i + j) & 1) ? ' ' : '.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
a = (c | (l <<= 1) | (r >>= 1)) & full;
|
||||
if (a != full)
|
||||
for (*(s = qs + --d) = 0, b = 1; b <= full; (*s)++, b <<= 1)
|
||||
if (!(b & a)) solve(d, b|c, b|l, b|r);
|
||||
# define attack(i, j) (hist[j] == i || abs(hist[j] - i) == col - j)
|
||||
for (int i = 0, j = 0; i < n; i++) {
|
||||
for (j = 0; j < col && !attack(i, j); j++);
|
||||
if (j < col) continue;
|
||||
|
||||
hist[col] = i;
|
||||
solve(n, col + 1, hist);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int n, char **argv)
|
||||
{
|
||||
if (n <= 1 || (nn = atoi(argv[1])) <= 0) nn = 8;
|
||||
|
||||
qs = calloc(nn, sizeof(int));
|
||||
full = (1U << nn) - 1;
|
||||
|
||||
solve(nn, 0, 0, 0);
|
||||
printf("\nSolutions: %d\n", count);
|
||||
return 0;
|
||||
if (n <= 1 || (n = atoi(argv[1])) <= 0) n = 8;
|
||||
int hist[n];
|
||||
solve(n, 0, hist);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,91 +1,38 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef unsigned int uint;
|
||||
uint count = 0;
|
||||
typedef uint32_t uint;
|
||||
uint full, *qs, count = 0, nn;
|
||||
|
||||
#define ulen sizeof(uint) * 8
|
||||
|
||||
/* could have defined as int solve(...), but void may have less
|
||||
chance to confuse poor optimizer */
|
||||
void solve(int n)
|
||||
void solve(uint d, uint c, uint l, uint r)
|
||||
{
|
||||
int cnt = 0;
|
||||
const uint full = -(int)(1 << (ulen - n));
|
||||
register uint bits, pos, *m, d, e;
|
||||
|
||||
uint b0, b1, l[32], r[32], c[32], mm[33] = {0};
|
||||
n -= 3;
|
||||
/* require second queen to be left of the first queen, so
|
||||
we ever only test half of the possible solutions. This
|
||||
is why we can't handle n=1 here */
|
||||
for (b0 = 1U << (ulen - n - 3); b0; b0 <<= 1) {
|
||||
for (b1 = b0 << 2; b1; b1 <<= 1) {
|
||||
d = n;
|
||||
/* c: columns occupied by previous queens.
|
||||
l: columns attacked by left diagonals
|
||||
r: by right diagnoals */
|
||||
c[n] = b0 | b1;
|
||||
l[n] = (b0 << 2) | (b1 << 1);
|
||||
r[n] = (b0 >> 2) | (b1 >> 1);
|
||||
|
||||
/* availabe columns on current row. m is stack */
|
||||
bits = *(m = mm + 1) = full & ~(l[n] | r[n] | c[n]);
|
||||
|
||||
while (bits) {
|
||||
/* d: depth, aka row. counting backwards
|
||||
because !d is often faster than d != n */
|
||||
while (d) {
|
||||
/* pos is right most nonzero bit */
|
||||
pos = -(int)bits & bits;
|
||||
|
||||
/* mark bit used. only put current bits
|
||||
on stack if not zero, so backtracking
|
||||
will skip exhausted rows (because reading
|
||||
stack variable is sloooow compared to
|
||||
registers) */
|
||||
if ((bits &= ~pos))
|
||||
*m++ = bits | d;
|
||||
|
||||
/* faster than l[d+1] = l[d]... */
|
||||
e = d--;
|
||||
l[d] = (l[e] | pos) << 1;
|
||||
r[d] = (r[e] | pos) >> 1;
|
||||
c[d] = c[e] | pos;
|
||||
|
||||
bits = full & ~(l[d] | r[d] | c[d]);
|
||||
|
||||
if (!bits) break;
|
||||
if (!d) { cnt++; break; }
|
||||
}
|
||||
/* Bottom of stack m is a zero'd field acting
|
||||
as sentinel. When saving to stack, left
|
||||
27 bits are the available columns, while
|
||||
right 5 bits is the depth. Hence solution
|
||||
is limited to size 27 board -- not that it
|
||||
matters in foreseeable future. */
|
||||
d = (bits = *--m) & 31U;
|
||||
bits &= ~31U;
|
||||
}
|
||||
}
|
||||
uint b, a, *s;
|
||||
if (!d) {
|
||||
count++;
|
||||
#if 0
|
||||
printf("\nNo. %d\n===========\n", count);
|
||||
for (a = 0; a < nn; a++, putchar('\n'))
|
||||
for (b = 0; b < nn; b++, putchar(' '))
|
||||
putchar(" -QQ"[((b == qs[a])<<1)|((a + b)&1)]);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
count = cnt * 2;
|
||||
|
||||
a = (c | (l <<= 1) | (r >>= 1)) & full;
|
||||
if (a != full)
|
||||
for (*(s = qs + --d) = 0, b = 1; b <= full; (*s)++, b <<= 1)
|
||||
if (!(b & a)) solve(d, b|c, b|l, b|r);
|
||||
}
|
||||
|
||||
int main(int c, char **v)
|
||||
int main(int n, char **argv)
|
||||
{
|
||||
int nn;
|
||||
if (c <= 1 || (nn = atoi(v[1])) <= 0) nn = 8;
|
||||
if (n <= 1 || (nn = atoi(argv[1])) <= 0) nn = 8;
|
||||
|
||||
if (nn > 27) {
|
||||
fprintf(stderr, "Value too large, abort\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Can't solve size 1 board; might as well skip 2 and 3 */
|
||||
if (nn < 4) count = nn == 1;
|
||||
else solve(nn);
|
||||
qs = calloc(nn, sizeof(int));
|
||||
full = (1U << nn) - 1;
|
||||
|
||||
solve(nn, 0, 0, 0);
|
||||
printf("\nSolutions: %d\n", count);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,69 +1,91 @@
|
|||
#include <stdio.h>
|
||||
#define MAXN 31
|
||||
#include <stdlib.h>
|
||||
|
||||
int nqueens(int n)
|
||||
typedef unsigned int uint;
|
||||
uint count = 0;
|
||||
|
||||
#define ulen sizeof(uint) * 8
|
||||
|
||||
/* could have defined as int solve(...), but void may have less
|
||||
chance to confuse poor optimizer */
|
||||
void solve(int n)
|
||||
{
|
||||
int q0,q1;
|
||||
int cols[MAXN], diagl[MAXN], diagr[MAXN], posibs[MAXN]; // Our backtracking 'stack'
|
||||
int num=0;
|
||||
//
|
||||
// The top level is two fors, to save one bit of symmetry in the enumeration by forcing second queen to
|
||||
// be AFTER the first queen.
|
||||
//
|
||||
for (q0=0; q0<n-2; q0++) {
|
||||
for (q1=q0+2; q1<n; q1++){
|
||||
int bit0 = 1<<q0;
|
||||
int bit1 = 1<<q1;
|
||||
int d=0; // d is our depth in the backtrack stack
|
||||
cols[0] = bit0 | bit1 | (-1<<n); // The -1 here is used to fill all 'coloumn' bits after n ...
|
||||
diagl[0]= (bit0<<1 | bit1)<<1;
|
||||
diagr[0]= (bit0>>1 | bit1)>>1;
|
||||
int cnt = 0;
|
||||
const uint full = -(int)(1 << (ulen - n));
|
||||
register uint bits, pos, *m, d, e;
|
||||
|
||||
// The variable posib contains the bitmask of possibilities we still have to try in a given row ...
|
||||
int posib = ~(cols[0] | diagl[0] | diagr[0]);
|
||||
uint b0, b1, l[32], r[32], c[32], mm[33] = {0};
|
||||
n -= 3;
|
||||
/* require second queen to be left of the first queen, so
|
||||
we ever only test half of the possible solutions. This
|
||||
is why we can't handle n=1 here */
|
||||
for (b0 = 1U << (ulen - n - 3); b0; b0 <<= 1) {
|
||||
for (b1 = b0 << 2; b1; b1 <<= 1) {
|
||||
d = n;
|
||||
/* c: columns occupied by previous queens.
|
||||
l: columns attacked by left diagonals
|
||||
r: by right diagnoals */
|
||||
c[n] = b0 | b1;
|
||||
l[n] = (b0 << 2) | (b1 << 1);
|
||||
r[n] = (b0 >> 2) | (b1 >> 1);
|
||||
|
||||
while (d >= 0) {
|
||||
while(posib) {
|
||||
int bit = posib & -posib; // The standard trick for getting the rightmost bit in the mask
|
||||
int ncols= cols[d] | bit;
|
||||
int ndiagl = (diagl[d] | bit) << 1;
|
||||
int ndiagr = (diagr[d] | bit) >> 1;
|
||||
int nposib = ~(ncols | ndiagl | ndiagr);
|
||||
posib^=bit; // Eliminate the tried possibility.
|
||||
/* availabe columns on current row. m is stack */
|
||||
bits = *(m = mm + 1) = full & ~(l[n] | r[n] | c[n]);
|
||||
|
||||
// The following is the main additional trick here, as recognizing solution can not be done using stack level (d),
|
||||
// since we save the depth+backtrack time at the end of the enumeration loop. However by noticing all coloumns are
|
||||
// filled (comparison to -1) we know a solution was reached ...
|
||||
// Notice also that avoiding an if on the ncols==-1 comparison is more efficient!
|
||||
num += ncols==-1;
|
||||
while (bits) {
|
||||
/* d: depth, aka row. counting backwards
|
||||
because !d is often faster than d != n */
|
||||
while (d) {
|
||||
/* pos is right most nonzero bit */
|
||||
pos = -(int)bits & bits;
|
||||
|
||||
if (nposib) {
|
||||
if (posib) { // This if saves stack depth + backtrack operations when we passed the last possibility in a row.
|
||||
posibs[d++] = posib; // Go lower in stack ..
|
||||
}
|
||||
cols[d] = ncols;
|
||||
diagl[d] = ndiagl;
|
||||
diagr[d] = ndiagr;
|
||||
posib = nposib;
|
||||
}
|
||||
}
|
||||
posib = posibs[--d]; // backtrack ...
|
||||
}
|
||||
}
|
||||
}
|
||||
return num*2;
|
||||
/* mark bit used. only put current bits
|
||||
on stack if not zero, so backtracking
|
||||
will skip exhausted rows (because reading
|
||||
stack variable is sloooow compared to
|
||||
registers) */
|
||||
if ((bits &= ~pos))
|
||||
*m++ = bits | d;
|
||||
|
||||
/* faster than l[d+1] = l[d]... */
|
||||
e = d--;
|
||||
l[d] = (l[e] | pos) << 1;
|
||||
r[d] = (r[e] | pos) >> 1;
|
||||
c[d] = c[e] | pos;
|
||||
|
||||
bits = full & ~(l[d] | r[d] | c[d]);
|
||||
|
||||
if (!bits) break;
|
||||
if (!d) { cnt++; break; }
|
||||
}
|
||||
/* Bottom of stack m is a zero'd field acting
|
||||
as sentinel. When saving to stack, left
|
||||
27 bits are the available columns, while
|
||||
right 5 bits is the depth. Hence solution
|
||||
is limited to size 27 board -- not that it
|
||||
matters in foreseeable future. */
|
||||
d = (bits = *--m) & 31U;
|
||||
bits &= ~31U;
|
||||
}
|
||||
}
|
||||
}
|
||||
count = cnt * 2;
|
||||
}
|
||||
|
||||
|
||||
main(int ac , char **av)
|
||||
int main(int c, char **v)
|
||||
{
|
||||
if(ac != 2) {
|
||||
printf("usage: nq n\n");
|
||||
return 1;
|
||||
}
|
||||
int n = atoi(av[1]);
|
||||
if(n<1 || n > MAXN) {
|
||||
printf("n must be between 2 and 31!\n");
|
||||
}
|
||||
printf("Number of solution for %d is %d\n",n,nqueens(n));
|
||||
int nn;
|
||||
if (c <= 1 || (nn = atoi(v[1])) <= 0) nn = 8;
|
||||
|
||||
if (nn > 27) {
|
||||
fprintf(stderr, "Value too large, abort\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Can't solve size 1 board; might as well skip 2 and 3 */
|
||||
if (nn < 4) count = nn == 1;
|
||||
else solve(nn);
|
||||
|
||||
printf("\nSolutions: %d\n", count);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
69
Task/N-queens-problem/C/n-queens-problem-5.c
Normal file
69
Task/N-queens-problem/C/n-queens-problem-5.c
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#include <stdio.h>
|
||||
#define MAXN 31
|
||||
|
||||
int nqueens(int n)
|
||||
{
|
||||
int q0,q1;
|
||||
int cols[MAXN], diagl[MAXN], diagr[MAXN], posibs[MAXN]; // Our backtracking 'stack'
|
||||
int num=0;
|
||||
//
|
||||
// The top level is two fors, to save one bit of symmetry in the enumeration by forcing second queen to
|
||||
// be AFTER the first queen.
|
||||
//
|
||||
for (q0=0; q0<n-2; q0++) {
|
||||
for (q1=q0+2; q1<n; q1++){
|
||||
int bit0 = 1<<q0;
|
||||
int bit1 = 1<<q1;
|
||||
int d=0; // d is our depth in the backtrack stack
|
||||
cols[0] = bit0 | bit1 | (-1<<n); // The -1 here is used to fill all 'coloumn' bits after n ...
|
||||
diagl[0]= (bit0<<1 | bit1)<<1;
|
||||
diagr[0]= (bit0>>1 | bit1)>>1;
|
||||
|
||||
// The variable posib contains the bitmask of possibilities we still have to try in a given row ...
|
||||
int posib = ~(cols[0] | diagl[0] | diagr[0]);
|
||||
|
||||
while (d >= 0) {
|
||||
while(posib) {
|
||||
int bit = posib & -posib; // The standard trick for getting the rightmost bit in the mask
|
||||
int ncols= cols[d] | bit;
|
||||
int ndiagl = (diagl[d] | bit) << 1;
|
||||
int ndiagr = (diagr[d] | bit) >> 1;
|
||||
int nposib = ~(ncols | ndiagl | ndiagr);
|
||||
posib^=bit; // Eliminate the tried possibility.
|
||||
|
||||
// The following is the main additional trick here, as recognizing solution can not be done using stack level (d),
|
||||
// since we save the depth+backtrack time at the end of the enumeration loop. However by noticing all coloumns are
|
||||
// filled (comparison to -1) we know a solution was reached ...
|
||||
// Notice also that avoiding an if on the ncols==-1 comparison is more efficient!
|
||||
num += ncols==-1;
|
||||
|
||||
if (nposib) {
|
||||
if (posib) { // This if saves stack depth + backtrack operations when we passed the last possibility in a row.
|
||||
posibs[d++] = posib; // Go lower in stack ..
|
||||
}
|
||||
cols[d] = ncols;
|
||||
diagl[d] = ndiagl;
|
||||
diagr[d] = ndiagr;
|
||||
posib = nposib;
|
||||
}
|
||||
}
|
||||
posib = posibs[--d]; // backtrack ...
|
||||
}
|
||||
}
|
||||
}
|
||||
return num*2;
|
||||
}
|
||||
|
||||
|
||||
main(int ac , char **av)
|
||||
{
|
||||
if(ac != 2) {
|
||||
printf("usage: nq n\n");
|
||||
return 1;
|
||||
}
|
||||
int n = atoi(av[1]);
|
||||
if(n<1 || n > MAXN) {
|
||||
printf("n must be between 2 and 31!\n");
|
||||
}
|
||||
printf("Number of solution for %d is %d\n",n,nqueens(n));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue