import 'package:test/test.dart';
List<List<int>> board = [];
bool isSafe(int size, int row, int col) {
for (int k = 0; k < row; k++) {
if (board[k][col] == 1) {
return false;
}
}
int i = row;
int j = col;
while (i >= 0 && j >= 0) {
if (board[i][j] == 1) {
return false;
}
j--;
i--;
}
i = row;
j = col;
while (i >= 0 && j < size) {
if (board[i][j] == 1) {
return false;
}
j++;
i--;
}
return true;
}
int solveNQueen(int row, int size) {
if (size == row) {
return 1;
}
int ways = 0;
for (int col = 0; col < size; col++) {
if (isSafe(size, row, col)) {
board[row][col] = 1;
ways += solveNQueen(row + 1, size);
board[row][col] = 0;
}
}
return ways;
}
void resetBoard(int boardSize) {
board = List.generate(boardSize, (i) => List.generate(boardSize, (j) => 0));
}
void main() {
test("N Queen #testcase 1", () {
resetBoard(1);
expect(solveNQueen(0, 1), equals(1));
});
test("N Queen #testcase 2", () {
resetBoard(2);
expect(solveNQueen(0, 2), equals(0));
});
test("N Queen #testcase 3", () {
resetBoard(3);
expect(solveNQueen(0, 3), equals(0));
});
test("N Queen #testcase 4", () {
resetBoard(4);
expect(solveNQueen(0, 4), equals(2));
});
test("N Queen #testcase 5", () {
resetBoard(5);
expect(solveNQueen(0, 5), equals(10));
});
test("N Queen #testcase 6", () {
resetBoard(6);
expect(solveNQueen(0, 6), equals(4));
});
test("N Queen #testcase 7", () {
resetBoard(7);
expect(solveNQueen(0, 7), equals(40));
});
test("N Queen #testcase 8", () {
resetBoard(8);
expect(solveNQueen(0, 8), equals(92));
});
test("N Queen #testcase 9", () {
resetBoard(9);
expect(solveNQueen(0, 9), equals(352));
});
test("N Queen #testcase 10", () {
resetBoard(10);
expect(solveNQueen(0, 10), equals(724));
});
}