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

Eight Queens puzzle More...

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

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.
 

Detailed Description

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.

Author
Unknown author
David Leal

Function Documentation

◆ isSafe()

template<size_t n>
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

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
59 {
60 int i = 0, j = 0;
61
62 // Check this row on left side
63 for (i = 0; i < col; i++) {
64 if (board[row][i]) {
65 return false;
66 }
67 }
68
69 // Check upper diagonal on left side
70 for (i = row, j = col; i >= 0 && j >= 0; i--, j--) {
71 if (board[i][j]) {
72 return false;
73 }
74 }
75 // Check lower diagonal on left side
76 for (i = row, j = col; j >= 0 && i < n; i++, j--) {
77 if (board[i][j]) {
78 return false;
79 }
80 }
81 return true;
82}

◆ main()

int main ( void )

Main function.

Returns
0 on exit
120 {
121 const int n = 4;
123 std::array<int, n>({0, 0, 0, 0}), std::array<int, n>({0, 0, 0, 0}),
124 std::array<int, n>({0, 0, 0, 0}), std::array<int, n>({0, 0, 0, 0})};
125
126 backtracking::n_queens::solveNQ<n>(board, 0);
127 return 0;
128}

◆ printSolution()

template<size_t n>
void backtracking::n_queens::printSolution ( 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
38 {
39 std::cout << "\n";
40 for (int i = 0; i < n; i++) {
41 for (int j = 0; j < n; j++) {
42 std::cout << "" << board[i][j] << " ";
43 }
44 std::cout << "\n";
45 }
46}

◆ solveNQ()

template<size_t n>
void backtracking::n_queens::solveNQ ( std::array< std::array< int, n >, n > board,
const int & col )

Solve n queens problem

Template Parameters
nnumber of matrix size
Parameters
boardmatrix where numbers are saved
colcurrent index in columns
91 {
92 if (col >= n) {
93 printSolution<n>(board);
94 return;
95 }
96
97 // Consider this column and try placing
98 // this queen in all rows one by one
99 for (int i = 0; i < n; i++) {
100 // Check if queen can be placed
101 // on board[i][col]
102 if (isSafe<n>(board, i, col)) {
103 // Place this queen in matrix
104 board[i][col] = 1;
105
106 // Recursive to place rest of the queens
107 solveNQ<n>(board, col + 1);
108
109 board[i][col] = 0; // backtrack
110 }
111 }
112}
Here is the call graph for this function: