forked from sysprog21/rv32emu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nqueens.c
176 lines (160 loc) · 4.53 KB
/
nqueens.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* N Queens Problem (number of Solutions)
* source: https://computerpuzzle.net/english/nqueens/
*/
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#define MAXSIZE 15
static int size_e;
static int board[MAXSIZE], *board_e, *board1, *board2;
static int MASK, TOPBIT, SIDEMASK, LASTMASK, ENDBIT;
static int64_t count8, count4, count2;
/* check unique solutions */
static void check()
{
int *own, *you, bit, ptn;
/* 90-degree rotation */
if (*board2 == 1) {
for (ptn = 2, own = board + 1; own <= board_e; own++, ptn <<= 1) {
bit = 1;
for (you = board_e; *you != ptn && *own >= bit; you--)
bit <<= 1;
if (*own > bit)
return;
if (*own < bit)
break;
}
if (own > board_e) {
count2++;
return;
}
}
/* 180-degree rotation */
if (*board_e == ENDBIT) {
for (you = board_e - 1, own = board + 1; own <= board_e; own++, you--) {
bit = 1;
for (ptn = TOPBIT; ptn != *you && *own >= bit; ptn >>= 1)
bit <<= 1;
if (*own > bit)
return;
if (*own < bit)
break;
}
if (own > board_e) {
count4++;
return;
}
}
/* 270-degree rotation */
if (*board1 == TOPBIT) {
for (ptn = TOPBIT >> 1, own = board + 1; own <= board_e;
own++, ptn >>= 1) {
bit = 1;
for (you = board; *you != ptn && *own >= bit; you++)
bit <<= 1;
if (*own > bit)
return;
if (*own < bit)
break;
}
}
count8++;
}
/* First queen is inside */
static void backtrack2(int y,
int left,
int down,
int right,
int bound1,
int bound2)
{
int bitmap = MASK & ~(left | down | right);
if (y == size_e) {
if (bitmap) {
if (!(bitmap & LASTMASK)) {
board[y] = bitmap;
check();
}
}
} else {
if (y < bound1) {
bitmap |= SIDEMASK;
bitmap ^= SIDEMASK;
} else if (y == bound2) {
if (!(down & SIDEMASK))
return;
if ((down & SIDEMASK) != SIDEMASK)
bitmap &= SIDEMASK;
}
while (bitmap) {
int bit;
bitmap ^= board[y] = bit = -bitmap & bitmap;
backtrack2(y + 1, (left | bit) << 1, down | bit, (right | bit) >> 1,
bound1, bound2);
}
}
}
/* First queen is in the corner */
static void backtrack1(int y, int left, int down, int right, int bound1)
{
int bitmap = MASK & ~(left | down | right);
if (y == size_e) {
if (bitmap) {
board[y] = bitmap;
count8++;
}
} else {
if (y < bound1) {
bitmap |= 2;
bitmap ^= 2;
}
while (bitmap) {
int bit;
bitmap ^= board[y] = bit = -bitmap & bitmap;
backtrack1(y + 1, (left | bit) << 1, down | bit, (right | bit) >> 1,
bound1);
}
}
}
/* Search of N-Queens */
static void nqueens(int size)
{
int bit;
/* Initialize */
count8 = count4 = count2 = 0;
size_e = size - 1;
board_e = &board[size_e];
TOPBIT = 1 << size_e;
MASK = (1 << size) - 1;
/* 0:000000001 */
/* 1:011111100 */
board[0] = 1;
for (int bound1 = 2; bound1 < size_e; bound1++) {
board[1] = bit = 1 << bound1;
backtrack1(2, (2 | bit) << 1, 1 | bit, bit >> 1, bound1);
}
/* 0:000001110 */
SIDEMASK = LASTMASK = TOPBIT | 1;
ENDBIT = TOPBIT >> 1;
for (int bound1 = 1, bound2 = size - 2; bound1 < bound2;
bound1++, bound2--) {
board1 = &board[bound1];
board2 = &board[bound2];
board[0] = bit = 1 << bound1;
backtrack2(1, bit << 1, bit, bit >> 1, bound1, bound2);
LASTMASK |= LASTMASK >> 1 | LASTMASK << 1;
ENDBIT >>= 1;
}
}
int main(void)
{
printf("<--- N-Queens Solutions --->\n");
printf(" N: Total Unique\n");
for (int size = 2; size <= MAXSIZE; size++) {
nqueens(size);
printf("%2d:%13" PRId64 "%13" PRId64 "\n", size,
count8 * 8 + count4 * 4 + count2 * 2 /* total solutions */,
count8 + count4 + count2 /* unique solutions */);
}
return 0;
}