30void PrintSol(
const std::array<std::array<int, n>, n> &board) {
31 for (
int i = 0; i < n; i++) {
32 for (
int j = 0; j < n; j++) {
33 std::cout << board[i][j] <<
" ";
35 std::cout << std::endl;
37 std::cout << std::endl;
38 if (n % 2 == 0 || (n % 2 == 1 && board[n / 2 + 1][0] != 1)) {
39 for (
int i = 0; i < n; i++) {
40 for (
int j = 0; j < n; j++) {
41 std::cout << board[j][i] <<
" ";
43 std::cout << std::endl;
45 std::cout << std::endl;
59bool CanIMove(
const std::array<std::array<int, n>, n> &board,
int row,
62 for (
int i = 0; i <= col; i++) {
63 if (board[row][i] == 1) {
68 for (
int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
69 if (board[i][j] == 1) {
74 for (
int i = row, j = col; i <= n - 1 && j >= 0; i++, j--) {
75 if (board[i][j] == 1) {
89void NQueenSol(std::array<std::array<int, n>, n> board,
int col) {
94 for (
int i = 0; i < n; i++) {
95 if (CanIMove<n>(board, i, col)) {
97 NQueenSol<n>(board, col + 1);
111 std::array<std::array<int, n>, n> board{};
114 for (
int i = 0; i <= n / 2 - 1; i++) {
115 if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) {
117 backtracking::n_queens_optimized::NQueenSol(board, 1);
122 for (
int i = 0; i <= n / 2; i++) {
123 if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) {
125 backtracking::n_queens_optimized::NQueenSol(board, 1);