> > backtracking.nqueen_print_all_solutions
Threads
nqueen_print_al ( 16546 ) - nqueen_print_al ( 16546 ) stack: main(backtracking/nqueen_print_all_solutions.cpp:101)
/**
 * @file
 * @brief [Eight Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle)
 * puzzle, printing all solutions
 *
 * @author [Himani Negi](https://github.com/Himani2000)
 * @author [David Leal](https://github.com/Panquesito7)
 *
 */
#include <array>     /// for std::array
#include <iostream>  /// for IO operations

/**
 * @namespace backtracking
 * @brief Backtracking algorithms
 */
namespace backtracking {
/**
 * @namespace n_queens_all_solutions
 * @brief Functions for the [Eight
 * Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle with all
 * solutions.
 */
namespace n_queens_all_solutions {
/**
 * @brief Utility function to print matrix
 * @tparam n number of matrix size
 * @param board matrix where numbers are saved
 */
template <size_t n>
void PrintSol(const std::array<std::array<int, n>, n>& board) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cout << board[i][j] << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

/**
 * @brief Check if a queen can be placed on the matrix
 * @tparam n number of matrix size
 * @param board matrix where numbers are saved
 * @param row current index in rows
 * @param col current index in columns
 * @returns `true` if queen can be placed on matrix
 * @returns `false` if queen can't be placed on matrix
 */
template <size_t n>
bool CanIMove(const std::array<std::array<int, n>, n>& board, int row,
              int col) {
    /// check in the row
    for (int i = 0; i < col; i++) {
        if (board[row][i] == 1) {
            return false;
        }
    }
    /// check the first diagonal
    for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
        if (board[i][j] == 1) {
            return false;
        }
    }
    /// check the second diagonal
    for (int i = row, j = col; i <= n - 1 && j >= 0; i++, j--) {
        if (board[i][j] == 1) {
            return false;
        }
    }
    return true;
}

/**
 * @brief Main function to solve the N Queens problem
 * @tparam n number of matrix size
 * @param board matrix where numbers are saved
 * @param col current index in columns
 */
template <size_t n>
void NQueenSol(std::array<std::array<int, n>, n> board, int col) {
    if (col >= n) {
        PrintSol(board);
        return;
    }
    for (int i = 0; i < n; i++) {
        if (CanIMove(board, i, col)) {
            board[i][col] = 1;
            NQueenSol(board, col + 1);
            board[i][col] = 0;
        }
    }
}
}  // namespace n_queens_all_solutions
}  // namespace backtracking

/**
 * @brief Main function
 * @returns 0 on exit
 */
int main() {
    const int n = 4;
    std::array<std::array<int, n>, n> board{0};

    backtracking::n_queens_all_solutions::NQueenSol(board, 0);
}
Variables All
No.FromNameValue
NANANANA
Output All Filter Merge
ProcessThread Filter
16546 nqueen_print_al 16546 nqueen_print_al
No.PNPIDTIDTNMessage
1nqueen_print_al1654616546nqueen_print_al0 0 1 0
2nqueen_print_al1654616546nqueen_print_al1 0 0 0
3nqueen_print_al1654616546nqueen_print_al0 0 0 1
4nqueen_print_al1654616546nqueen_print_al0 1 0 0
5nqueen_print_al1654616546nqueen_print_al
6nqueen_print_al1654616546nqueen_print_al0 1 0 0
7nqueen_print_al1654616546nqueen_print_al0 0 0 1
8nqueen_print_al1654616546nqueen_print_al1 0 0 0
9nqueen_print_al1654616546nqueen_print_al0 0 1 0
10nqueen_print_al1654616546nqueen_print_al
END 0 0 0 00
Project:algorithms-cpp
Update:20240212
Commit:2dadbf73f
Source Code:backtracking.nqueen_print_all_solutions
BuildTool:cpp17
Compiler:cpp17
Runtime:Openjdk17
System:MySystemD
Kernel:Linux5.10.211
Cpu:Intel:Corei7-7700K
Machine:AwesomeMachine