N-queens problem: Difference between revisions

Line 803:
=={{header|D}}==
From the C solution.
<lang d>import std.stdio: write, writeln, writefln;
 
enum int SIDE = 8;
int[SIDE] bboard;
 
nothrow bool unsafe(int y) {
immutable int x = bboard[y];
foreach (i; 1 .. y+1) {
int t = bboard[y - i];
if ((t == x) || (t == x - i) || (t == x + i))
return true;
Line 819:
}
 
void show_boardshowBoard() {
static int s = 0;
writeflnwriteln("\nSolution #%d", ++s);
foreach (y; 0 .. SIDE) {
foreach (x; 0 .. SIDE)
write(bboard[y] == x ? "|Q"'*' : "|_"'.');
writeln("|");
}
}
Line 831:
void main() {
int y = 0;
bboard[0] = -1;
 
while (y >= 0) {
do {
bboard[y]++;
} while (bboard[y] < SIDE && unsafe(y));
 
if (b[y] < SIDE) {
if (board[y] < (SIDE - 1)) {
if (y < (SIDE b[++y]- = -1;))
} else { board[++y] = -1;
show_board();else
} showBoard();
} else {
y--;
}
}
}</lang>
===Brute force version===
 
Brute force version
<lang d>auto nqueens(int base) {
int total = base * base;
Anonymous user