aboutsummaryrefslogtreecommitdiffstats
path: root/lcc/tst/8q.c
blob: e55ba1a63ecf24f7012bdfa6eee7461162092fc7 (plain)
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
int up[15], down[15], rows[8], x[8];
int queens(), print();

main()
{
	int i;

	for (i = 0; i < 15; i++)
		up[i] = down[i] = 1;
	for (i = 0; i < 8; i++)
		rows[i] = 1;
	queens(0);
	return 0;
}

queens(c)
{
	int r;

	for (r = 0; r < 8; r++)
		if (rows[r] && up[r-c+7] && down[r+c]) {
			rows[r] = up[r-c+7] = down[r+c] = 0;
			x[c] = r;
			if (c == 7)
				print();
			else
				queens(c + 1);
			rows[r] = up[r-c+7] = down[r+c] = 1;
		}
}

print()
{
	int k;

	for (k = 0; k < 8; k++)
		printf("%c ", x[k]+'1');
	printf("\n");
}