n_queens_all_solution_optimised.cpp File Reference

N queens all optimized More...

`#include <array>`
`#include <iostream>`
Include dependency graph for n_queens_all_solution_optimised.cpp:

## Namespaces

| backtracking
| Backtracking algorithms.
| n_queens_optimized
| Functions for Eight Queens puzzle optimized.

## Functions

template<size_t n>
void | backtracking::n_queens_optimized::PrintSol (const std::array< std::array< int, n >, n > &board)
template<size_t n>
bool | backtracking::n_queens_optimized::CanIMove (const std::array< std::array< int, n >, n > &board, int row, int col)
template<size_t n>
void | backtracking::n_queens_optimized::NQueenSol (std::array< std::array< int, n >, n > board, int col)
int | main ()
| Main function. More...

## Detailed Description

N queens all optimized

Author
  Sombit Bose
  David Leal

## Function Documentation

## CanIMove()

template<size_t n>

bool backtracking::n_queens_optimized::CanIMove | ( | const std::array< std::array< int, n >, n > & | board,
|  | int | row,
|  | int | col
| ) |  |

Check if a queen can be placed on matrix

Template Parameters

  n | number of matrix size

Parameters

  board | matrix where numbers are saved
  row | current index in rows
  col | current index in columns

Returns
  `true` if queen can be placed on matrix
  `false` if queen can't be placed on matrix

check in the row

check the first diagonal

check the second diagonal

{
/// 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;
}
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
const int n = 4;
std::array<std::array<int, n>, n> board{};
if (n % 2 == 0) {
for (int i = 0; i <= n / 2 - 1; i++) { // 😎
if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) {
board[i][0] = 1;
backtracking::n_queens_optimized::NQueenSol(board, 1);
board[i][0] = 0;
}
}
} else {
for (int i = 0; i <= n / 2; i++) { // 😏
if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) {
board[i][0] = 1;
backtracking::n_queens_optimized::NQueenSol(board, 1);
board[i][0] = 0;
}
}
}
return 0;
}
std::array
backtracking::n_queens_optimized::NQueenSol
void NQueenSol(std::array< std::array< int, n >, n > board, int col)
Definition: n_queens_all_solution_optimised.cpp:89

## NQueenSol()

template<size_t n>

void backtracking::n_queens_optimized::NQueenSol | ( | std::array< std::array< int, n >, n > | board,
|  | int | col
| ) |  |

Solve n queens problem

Template Parameters

  n | number of matrix size

Parameters

  board | matrix where numbers are saved
  col | current index in columns

{
if (col >= n) {
PrintSol<n>(board);
return;
}
for (int i = 0; i < n; i++) {
if (CanIMove<n>(board, i, col)) {
board[i][col] = 1;
NQueenSol<n>(board, col + 1);
board[i][col] = 0;
}
}
}
Here is the call graph for this function:

## PrintSol()

template<size_t n>

void backtracking::n_queens_optimized::PrintSol | ( | const std::array< std::array< int, n >, n > & | board | ) |

Utility function to print matrix

Template Parameters

  n | number of matrix size

Parameters

  board | matrix where numbers are saved

{
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;
if (n % 2 == 0 || (n % 2 == 1 && board[n / 2 + 1][0] != 1)) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cout << board[j][i] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
}
std::cout
std::endl
T endl(T... args)
Here is the call graph for this function: