0

I came across well-known N-Queen problem and I was wondering how to write a program to calculate number of possibilities in this particular problem. My program can find solution fast for really small N's (since it's heuristic).

I'd also like to know how to represent such big numbers in C. Are there any algorithms for really big numbers? Anytime I write and implementation of my own arithmetic I get i. e. quadratic multiplication with tons of memory allocation what cannot be fast. Thank you in advance for exhaustive answer.

bottaio
  • 4,963
  • 3
  • 19
  • 43
  • What is your definition of "reaaly small N's"? 2? 4? 8? – Degustaf Jan 16 '15 at 19:15
  • 2
    An exhaustive answer to a minimally composed question? I don't like putting in more effort than the OP shows. – Weather Vane Jan 16 '15 at 19:30
  • What do you consider a "Big" number? Is a "Big" number an integer or approximate real number? – user3344003 Jan 17 '15 at 00:14
  • for the Queens problem, there are only 8 rows and 8 columns so a grid of 8x8 is large enough and only need to mark each point in the grid to indicate if a queen can reach that point. there are no complicated algorithms, just a simple set of nested loops with the innermost loop checking if a queen can be placed at a specific location and if such location does not interfere with any of the exiting queens. – user3629249 Jan 17 '15 at 05:27
  • a heuristic tree could be used with min/max and alpha/beta and branch pruning, but for the simple situation of only Queens and no look ahead moves to be made, a simple brute force algorithm will work very nicely. – user3629249 Jan 17 '15 at 05:30
  • you might want to look at this: for a brute force method. And you might want to look at this for indepth discussion of several better algorithms: – user3629249 Jan 17 '15 at 05:35
  • you could use the bignum library, available at: – user3629249 Jan 17 '15 at 05:37

1 Answers1

0
here is a nice solution, using recursion 
(taken from: <http://rosettacode.org/wiki/N-queens_problem#C>)

#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)
{
    uint b, a, *s;
    if (!d)  // exit condition
    {
        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)]);
            } // end for
        } // end for
#endif
        return;
    } // end if

    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);
            } // end if
        } // end for
    } // end if
} // end function: solve


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;
}  // end function: main
user3629249
  • 16,402
  • 1
  • 16
  • 17