summary refs log tree commit diff
path: root/minic/test/queen.c
blob: d9d93fc4248824513d34ea93984aa47baf17d42d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>

int N;
int *t;

print() {
	int x;
	int y;

	y = 0;
	while (y < 8) {
		x = 0;
		while (x < 8) {
			if (t[x + 8*y])
				printf(" Q");
			else
				printf(" .");
			x++;
		}
		printf("\n");
		y++;
	}
	printf("\n");
}

chk(int x, int y) {
	int i;
	int r;

	r = 0;
	i = 0;
	while (i < 8) {
		r = r + t[x + 8*i];
		r = r + t[i + 8*y];
		if (x+i < 8 & y+i < 8)
			r = r + t[x+i + 8*(y+i)];
		if (x+i < 8 & y-i >= 0)
			r = r + t[x+i + 8*(y-i)];
		if (x-i >= 0 & y+i < 8)
			r = r + t[x-i + 8*(y+i)];
		if (x-i >= 0 & y-i >= 0)
			r = r + t[x-i + 8*(y-i)];
		if (r)
			return 1;
		i++;
	}
	return 0;
}

go(int n, int x, int y) {
	if (n == 8) {
		print();
		N++;
		return 0;
	}
	while (y < 8) {
		while (x < 8) {
			if (chk(x, y) == 0) {
				t[x + 8*y]++;
				go(n+1, x, y);
				t[x + 8*y]--;
			}
			x++;
		}
		x = 0;
		y++;
	}
}

main() {
	t = calloc(64, sizeof(int));
	go(0, 0, 0);
	printf("found %d solutions\n", N);
}