using
System;
using
System.Linq;
class
GFG {
static
int
N = 8;
static
void
printSolution(
int
[, ] board)
{
for
(
int
i = 0; i < N; i++) {
for
(
int
j = 0; j < N; j++)
Console.Write(
"{0} "
, board[i, j]);
Console.WriteLine();
}
}
static
bool
isSafe(
int
row,
int
col,
int
[, ] slashCode,
int
[, ] backslashCode,
bool
[] rowLookup,
bool
[] slashCodeLookup,
bool
[] backslashCodeLookup)
{
if
(slashCodeLookup[slashCode[row, col]]
|| backslashCodeLookup[backslashCode[row, col]]
|| rowLookup[row])
return
false
;
return
true
;
}
static
bool
solveNQueensUtil(
int
[, ] board,
int
col,
int
[, ] slashCode,
int
[, ] backslashCode,
bool
[] rowLookup,
bool
[] slashCodeLookup,
bool
[] backslashCodeLookup)
{
if
(col >= N)
return
true
;
for
(
int
i = 0; i < N; i++) {
if
(isSafe(i, col, slashCode, backslashCode,
rowLookup, slashCodeLookup,
backslashCodeLookup)) {
board[i, col] = 1;
rowLookup[i] =
true
;
slashCodeLookup[slashCode[i, col]] =
true
;
backslashCodeLookup[backslashCode[i, col]]
=
true
;
if
(solveNQueensUtil(
board, col + 1, slashCode,
backslashCode, rowLookup,
slashCodeLookup,
backslashCodeLookup))
return
true
;
board[i, col] = 0;
rowLookup[i] =
false
;
slashCodeLookup[slashCode[i, col]] =
false
;
backslashCodeLookup[backslashCode[i, col]]
=
false
;
}
}
return
false
;
}
static
bool
solveNQueens()
{
int
[, ] board =
new
int
[N, N];
int
[, ] slashCode =
new
int
[N, N];
int
[, ] backslashCode =
new
int
[N, N];
bool
[] rowLookup =
new
bool
[N];
bool
[] slashCodeLookup =
new
bool
[2 * N - 1];
bool
[] backslashCodeLookup =
new
bool
[2 * N - 1];
for
(
int
r = 0; r < N; r++) {
for
(
int
c = 0; c < N; c++) {
slashCode[r, c] = r + c;
backslashCode[r, c] = r - c + N - 1;
}
}
if
(!solveNQueensUtil(board, 0, slashCode,
backslashCode, rowLookup,
slashCodeLookup,
backslashCodeLookup)) {
Console.WriteLine(
"Solution does not exist"
);
return
false
;
}
printSolution(board);
return
true
;
}
public
static
void
Main() { solveNQueens(); }
}