Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
nqueen_print_all_solutions.cpp File Reference

Eight Queens puzzle, printing all solutions More...

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

Namespaces

namespace  backtracking
 for vector container
 
namespace  n_queens_all_solutions
 Functions for the Eight Queens puzzle with all solutions.
 

Functions

template<size_t n>
void backtracking::n_queens_all_solutions::PrintSol (const std::array< std::array< int, n >, n > &board)
 Utility function to print matrix.
 
template<size_t n>
bool backtracking::n_queens_all_solutions::CanIMove (const std::array< std::array< int, n >, n > &board, int row, int col)
 Check if a queen can be placed on the matrix.
 
template<size_t n>
void backtracking::n_queens_all_solutions::NQueenSol (std::array< std::array< int, n >, n > board, int col)
 Main function to solve the N Queens problem.
 
int main ()
 Main function.
 

Detailed Description

Eight Queens puzzle, printing all solutions

Author
Himani Negi
David Leal

Function Documentation

◆ CanIMove()

template<size_t n>
bool backtracking::n_queens_all_solutions::CanIMove ( const std::array< std::array< int, n >, n > & board,
int row,
int col )

Check if a queen can be placed on the matrix.

Template Parameters
nnumber of matrix size
Parameters
boardmatrix where numbers are saved
rowcurrent index in rows
colcurrent 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

52 {
53 /// check in the row
54 for (int i = 0; i < col; i++) {
55 if (board[row][i] == 1) {
56 return false;
57 }
58 }
59 /// check the first diagonal
60 for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
61 if (board[i][j] == 1) {
62 return false;
63 }
64 }
65 /// check the second diagonal
66 for (int i = row, j = col; i <= n - 1 && j >= 0; i++, j--) {
67 if (board[i][j] == 1) {
68 return false;
69 }
70 }
71 return true;
72}

◆ main()

int main ( void )

Main function.

Returns
0 on exit
101 {
102 const int n = 4;
104
106}
void NQueenSol(std::array< std::array< int, n >, n > board, int col)
Main function to solve the N Queens problem.
Definition nqueen_print_all_solutions.cpp:81

◆ NQueenSol()

template<size_t n>
void backtracking::n_queens_all_solutions::NQueenSol ( std::array< std::array< int, n >, n > board,
int col )

Main function to solve the N Queens problem.

Template Parameters
nnumber of matrix size
Parameters
boardmatrix where numbers are saved
colcurrent index in columns
81 {
82 if (col >= n) {
83 PrintSol(board);
84 return;
85 }
86 for (int i = 0; i < n; i++) {
87 if (CanIMove(board, i, col)) {
88 board[i][col] = 1;
89 NQueenSol(board, col + 1);
90 board[i][col] = 0;
91 }
92 }
93}
void PrintSol(const std::array< std::array< int, n >, n > &board)
Definition n_queens_all_solution_optimised.cpp:30
void NQueenSol(std::array< std::array< int, n >, n > board, int col)
Definition n_queens_all_solution_optimised.cpp:89
bool CanIMove(const std::array< std::array< int, n >, n > &board, int row, int col)
Definition n_queens_all_solution_optimised.cpp:59

◆ PrintSol()

template<size_t n>
void backtracking::n_queens_all_solutions::PrintSol ( const std::array< std::array< int, n >, n > & board)

Utility function to print matrix.

Template Parameters
nnumber of matrix size
Parameters
boardmatrix where numbers are saved
31 {
32 for (int i = 0; i < n; i++) {
33 for (int j = 0; j < n; j++) {
34 std::cout << board[i][j] << " ";
35 }
37 }
39}
T endl(T... args)
Here is the call graph for this function: