![]() |
TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Eight Queens puzzle More...
#include <array>#include <iostream>Go to the source code of this file.
Namespaces | |
| namespace | backtracking |
| for vector container | |
| namespace | n_queens |
| Functions for Eight Queens puzzle. | |
Functions | |
| template<size_t n> | |
| void | backtracking::n_queens::printSolution (const std::array< std::array< int, n >, n > &board) |
| template<size_t n> | |
| bool | backtracking::n_queens::isSafe (const std::array< std::array< int, n >, n > &board, const int &row, const int &col) |
| template<size_t n> | |
| void | backtracking::n_queens::solveNQ (std::array< std::array< int, n >, n > board, const int &col) |
| int | main () |
| Main function. | |
Eight Queens puzzle
The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other; thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general n queens problem of placing n non-attacking queens on an n×n chessboard, for which solutions exist for all natural numbers n with the exception of n = 2 and n = 3.
Definition in file n_queens.cpp.
| bool backtracking::n_queens::isSafe | ( | const std::array< std::array< int, n >, n > & | board, |
| const int & | row, | ||
| const int & | col ) |
Check if a queen can be placed on matrix
| n | number of matrix size |
| board | matrix where numbers are saved |
| row | current index in rows |
| col | current index in columns |
Definition at line 58 of file n_queens.cpp.
| int main | ( | void | ) |
Main function.
Definition at line 120 of file n_queens.cpp.
| void backtracking::n_queens::printSolution | ( | const std::array< std::array< int, n >, n > & | board | ) |
Utility function to print matrix
| n | number of matrix size |
| board | matrix where numbers are saved |
Definition at line 38 of file n_queens.cpp.
| void backtracking::n_queens::solveNQ | ( | std::array< std::array< int, n >, n > | board, |
| const int & | col ) |
Solve n queens problem
| n | number of matrix size |
| board | matrix where numbers are saved |
| col | current index in columns |
Definition at line 91 of file n_queens.cpp.