Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- int _doit(int n, int c, int *h)
- {
- int count;
- int i;
- int j;
- count = (i = -1);
- while ((c - n) && (j = !(++i < n) -1)) {
- while (++j < c && !(h[j] == i || abs(h[j] - i) == c - j));
- if (j < c) continue;
- h[c] = i;
- count += _doit(n, c + 1, h);
- }
- return (++count + !(c - n));
- }
- int count_valid_queen_placement(int n) {
- int h[n];
- return _doit(n, 0, h);
- }
- int main(int n, char **argv)
- {
- if (n <= 1 || (n = atoi(argv[1])) <= 0) n = 8;
- printf("%d\n", count_valid_queen_placement(n));
- }
Advertisement
Add Comment
Please, Sign In to add comment