|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Eight Queens puzzle More...
#include <array>#include <iostream>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.
| 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 |
true if queen can be placed on matrix false if queen can't be placed on matrix | int main | ( | void | ) |
Main function.
| 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 |
| 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 |