Skip to content

Commit 2c13a7a

Browse files
Panquesito7github-actions
and
github-actions
authored
[fix/docs]: Improve backtracking/n_queens_all_solution_optimised.cpp (TheAlgorithms#1041)
* updating DIRECTORY.md * [fix/docs]: Improve backtracking/n_queens_... ...all_solution_optimised.cpp * fix: Remove function-like macros * clang-format and clang-tidy fixes for 689b788 Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 7c64998 commit 2c13a7a

File tree

2 files changed

+147
-87
lines changed

2 files changed

+147
-87
lines changed

backtracking/n_queens_all_solution_optimised.cpp

+90-32
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,129 @@
1+
/**
2+
* @file
3+
* @brief [N queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) all
4+
* optimized
5+
*
6+
* @author [Sombit Bose](https://github.com/deadshotsb)
7+
* @author [David Leal](https://github.com/Panquesito7)
8+
*/
9+
10+
#include <array>
111
#include <iostream>
2-
#define n 4
3-
#define inc_loop(var, start, stop) for (int var = start; var <= stop; var++)
4-
#define dec_loop(var, start, stop) for (int var = start; var >= stop; var--)
5-
void PrintSol(int Board[n][n]) {
6-
inc_loop(i, 0, n - 1) {
7-
inc_loop(j, 0, n - 1) std::cout << Board[i][j] << " ";
12+
13+
/**
14+
* @namespace backtracking
15+
* @brief Backtracking algorithms
16+
*/
17+
namespace backtracking {
18+
/**
19+
* @namespace n_queens_optimized
20+
* @brief Functions for [Eight
21+
* Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle optimized.
22+
*/
23+
namespace n_queens_optimized {
24+
/**
25+
* Utility function to print matrix
26+
* @tparam n number of matrix size
27+
* @param board matrix where numbers are saved
28+
*/
29+
template <size_t n>
30+
void PrintSol(const std::array<std::array<int, n>, n> &board) {
31+
for (int i = 0; i < n; i++) {
32+
for (int j = 0; j < n; j++) {
33+
std::cout << board[i][j] << " ";
34+
}
835
std::cout << std::endl;
936
}
1037
std::cout << std::endl;
11-
if (n % 2 == 0 || (n % 2 == 1 && Board[n / 2 + 1][0] != 1)) {
12-
inc_loop(i, 0, n - 1) {
13-
dec_loop(j, n - 1, 0) std::cout << Board[i][j] << " ";
38+
if (n % 2 == 0 || (n % 2 == 1 && board[n / 2 + 1][0] != 1)) {
39+
for (int i = 0; i < n; i++) {
40+
for (int j = 0; j < n; j++) {
41+
std::cout << board[j][i] << " ";
42+
}
1443
std::cout << std::endl;
1544
}
1645
std::cout << std::endl;
1746
}
1847
}
1948

20-
bool CanIMove(int Board[n][n], int row, int col) {
49+
/**
50+
* Check if a queen can be placed on matrix
51+
* @tparam n number of matrix size
52+
* @param board matrix where numbers are saved
53+
* @param row current index in rows
54+
* @param col current index in columns
55+
* @returns `true` if queen can be placed on matrix
56+
* @returns `false` if queen can't be placed on matrix
57+
*/
58+
template <size_t n>
59+
bool CanIMove(const std::array<std::array<int, n>, n> &board, int row,
60+
int col) {
2161
/// check in the row
22-
inc_loop(i, 0, col - 1) {
23-
if (Board[row][i] == 1)
62+
for (int i = 0; i <= col; i++) {
63+
if (board[row][i] == 1) {
2464
return false;
65+
}
2566
}
2667
/// check the first diagonal
2768
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
28-
if (Board[i][j] == 1)
69+
if (board[i][j] == 1) {
2970
return false;
71+
}
3072
}
3173
/// check the second diagonal
3274
for (int i = row, j = col; i <= n - 1 && j >= 0; i++, j--) {
33-
if (Board[i][j] == 1)
75+
if (board[i][j] == 1) {
3476
return false;
77+
}
3578
}
3679
return true;
3780
}
3881

39-
void NQueenSol(int Board[n][n], int col) {
82+
/**
83+
* Solve n queens problem
84+
* @tparam n number of matrix size
85+
* @param board matrix where numbers are saved
86+
* @param col current index in columns
87+
*/
88+
template <size_t n>
89+
void NQueenSol(std::array<std::array<int, n>, n> board, int col) {
4090
if (col >= n) {
41-
PrintSol(Board);
91+
PrintSol<n>(board);
4292
return;
4393
}
44-
inc_loop(i, 0, n - 1) {
45-
if (CanIMove(Board, i, col)) {
46-
Board[i][col] = 1;
47-
NQueenSol(Board, col + 1);
48-
Board[i][col] = 0;
94+
for (int i = 0; i < n; i++) {
95+
if (CanIMove<n>(board, i, col)) {
96+
board[i][col] = 1;
97+
NQueenSol<n>(board, col + 1);
98+
board[i][col] = 0;
4999
}
50100
}
51101
}
102+
} // namespace n_queens_optimized
103+
} // namespace backtracking
52104

105+
/**
106+
* @brief Main function
107+
* @returns 0 on exit
108+
*/
53109
int main() {
54-
int Board[n][n] = {0};
110+
const int n = 4;
111+
std::array<std::array<int, n>, n> board{};
112+
55113
if (n % 2 == 0) {
56-
inc_loop(i, 0, n / 2 - 1) {
57-
if (CanIMove(Board, i, 0)) {
58-
Board[i][0] = 1;
59-
NQueenSol(Board, 1);
60-
Board[i][0] = 0;
114+
for (int i = 0; i <= n / 2 - 1; i++) { // 😎
115+
if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) {
116+
board[i][0] = 1;
117+
backtracking::n_queens_optimized::NQueenSol(board, 1);
118+
board[i][0] = 0;
61119
}
62120
}
63121
} else {
64-
inc_loop(i, 0, n / 2) {
65-
if (CanIMove(Board, i, 0)) {
66-
Board[i][0] = 1;
67-
NQueenSol(Board, 1);
68-
Board[i][0] = 0;
122+
for (int i = 0; i <= n / 2; i++) { // 😏
123+
if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) {
124+
board[i][0] = 1;
125+
backtracking::n_queens_optimized::NQueenSol(board, 1);
126+
board[i][0] = 0;
69127
}
70128
}
71129
}

sorting/pancake_sort.cpp

+57-55
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
/**
22
* @file
3-
* @brief pancake sort sorts a disordered stack of pancakes by flipping any number of pancakes using a spatula using minimum number of flips.
3+
* @brief pancake sort sorts a disordered stack of pancakes by flipping any
4+
* number of pancakes using a spatula using minimum number of flips.
45
*
56
* @details
6-
* Unlike a traditional sorting algorithm, which attempts to sort with the fewest comparisons possible,
7-
* the goal is to sort the sequence in as few reversals as possible.
8-
* Overall time complexity of pancake sort is O(n^2)
9-
* For example: example 1:-
10-
* Disordered pancake sizes: {2,5,3,7,8}
11-
* Sorted: {2,3,5,7,8}
12-
* For example: example 2:-
13-
* Disordered pancake sizes: {22,51,37,73,81}
14-
* Sorted: {22,37,51,73,81}
7+
* Unlike a traditional sorting algorithm, which attempts to sort with the
8+
* fewest comparisons possible, the goal is to sort the sequence in as few
9+
* reversals as possible. Overall time complexity of pancake sort is O(n^2) For
10+
* example: example 1:- Disordered pancake sizes: {2,5,3,7,8} Sorted:
11+
* {2,3,5,7,8} For example: example 2:- Disordered pancake sizes:
12+
* {22,51,37,73,81} Sorted: {22,37,51,73,81}
1513
* @author [Divyansh Gupta](https://github.com/divyansh12323)
1614
* @see more on [Pancake sort](https://en.wikipedia.org/wiki/Pancake_sorting)
17-
* @see related problem at [Leetcode](https://leetcode.com/problems/pancake-sorting/)
18-
*/
15+
* @see related problem at
16+
* [Leetcode](https://leetcode.com/problems/pancake-sorting/)
17+
*/
1918

20-
#include <iostream> // for io operations
21-
#include <vector> // for std::vector
2219
#include <algorithm> // for std::is_sorted
2320
#include <cassert> // for std::assert
21+
#include <iostream> // for io operations
22+
#include <vector> // for std::vector
2423

2524
/**
2625
* @namespace sorting
@@ -29,52 +28,54 @@
2928
namespace sorting {
3029
/**
3130
* @namespace pancake_sort
32-
* @brief Functions for [Pancake sort](https://en.wikipedia.org/wiki/Pancake_sorting) algorithm
31+
* @brief Functions for [Pancake
32+
* sort](https://en.wikipedia.org/wiki/Pancake_sorting) algorithm
3333
*/
3434
namespace pancake_sort {
35-
/**
36-
* @brief This implementation is for reversing elements in a a C-style array .
37-
* @param [start,end] arr our vector of elements.
38-
* @param start starting index of array
39-
* @param end ending index of array
40-
* @returns void
41-
*/
42-
template<typename T>
43-
void reverse(std::vector<T> &arr, int start, int end) {
44-
T temp; //Temporary variable
45-
while (start <= end) {
46-
temp = arr[start];
47-
arr[start] = arr[end];
48-
arr[end] = temp;
49-
start++;
50-
end--;
51-
}
35+
/**
36+
* @brief This implementation is for reversing elements in a a C-style array .
37+
* @param [start,end] arr our vector of elements.
38+
* @param start starting index of array
39+
* @param end ending index of array
40+
* @returns void
41+
*/
42+
template <typename T>
43+
void reverse(std::vector<T> &arr, int start, int end) {
44+
T temp; // Temporary variable
45+
while (start <= end) {
46+
temp = arr[start];
47+
arr[start] = arr[end];
48+
arr[end] = temp;
49+
start++;
50+
end--;
5251
}
53-
/**
54-
* @brief This implementation is for a C-style array input that gets modified in place.
55-
* @param [start,end] arr our vector of elements.
56-
* @param size size of given array
57-
* @returns 0 on exit
58-
*/
59-
template<typename T>
60-
int pancakeSort(std::vector<T> &arr, int size) {
61-
for (int i = size; i > 1; --i) {
62-
int max_index = 0, j; //intialize some variables.
63-
T max_value = 0;
64-
for (j = 0; j < i; j++) {
65-
if (arr[j] >= max_value) {
66-
max_value = arr[j];
67-
max_index = j;
68-
}
69-
}
70-
if (max_index != i - 1) //check for reversing
71-
{
72-
reverse(arr, 0, max_index);
73-
reverse(arr, 0, i - 1);
52+
}
53+
/**
54+
* @brief This implementation is for a C-style array input that gets modified in
55+
* place.
56+
* @param [start,end] arr our vector of elements.
57+
* @param size size of given array
58+
* @returns 0 on exit
59+
*/
60+
template <typename T>
61+
int pancakeSort(std::vector<T> &arr, int size) {
62+
for (int i = size; i > 1; --i) {
63+
int max_index = 0, j = 0; // intialize some variables.
64+
T max_value = 0;
65+
for (j = 0; j < i; j++) {
66+
if (arr[j] >= max_value) {
67+
max_value = arr[j];
68+
max_index = j;
7469
}
7570
}
76-
return 0;
71+
if (max_index != i - 1) // check for reversing
72+
{
73+
reverse(arr, 0, max_index);
74+
reverse(arr, 0, i - 1);
75+
}
7776
}
77+
return 0;
78+
}
7879
} // namespace pancake_sort
7980
} // namespace sorting
8081

@@ -98,7 +99,8 @@ static void test() {
9899
// example 2: vector of double
99100
const int size2 = 8;
100101
std::cout << "\nTest 2- as std::vector<double>...";
101-
std::vector<double> arr2 = {23.56, 10.62, 200.78, 111.484, 3.9, 1.2, 61.77, 79.6};
102+
std::vector<double> arr2 = {23.56, 10.62, 200.78, 111.484,
103+
3.9, 1.2, 61.77, 79.6};
102104
sorting::pancake_sort::pancakeSort(arr2, size2);
103105
assert(std::is_sorted(arr2.begin(), arr2.end()));
104106
std::cout << "Passed\n";

0 commit comments

Comments
 (0)