mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
[fix/docs]: Improve backtracking/n_queens_all_solution_optimised.cpp (#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 689b788c
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
7c64998d42
commit
2c13a7a376
@ -1,71 +1,129 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief [N queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) all
|
||||
* optimized
|
||||
*
|
||||
* @author [Sombit Bose](https://github.com/deadshotsb)
|
||||
* @author [David Leal](https://github.com/Panquesito7)
|
||||
*/
|
||||
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#define n 4
|
||||
#define inc_loop(var, start, stop) for (int var = start; var <= stop; var++)
|
||||
#define dec_loop(var, start, stop) for (int var = start; var >= stop; var--)
|
||||
void PrintSol(int Board[n][n]) {
|
||||
inc_loop(i, 0, n - 1) {
|
||||
inc_loop(j, 0, n - 1) std::cout << Board[i][j] << " ";
|
||||
|
||||
/**
|
||||
* @namespace backtracking
|
||||
* @brief Backtracking algorithms
|
||||
*/
|
||||
namespace backtracking {
|
||||
/**
|
||||
* @namespace n_queens_optimized
|
||||
* @brief Functions for [Eight
|
||||
* Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle optimized.
|
||||
*/
|
||||
namespace n_queens_optimized {
|
||||
/**
|
||||
* Utility function to print matrix
|
||||
* @tparam n number of matrix size
|
||||
* @param board matrix where numbers are saved
|
||||
*/
|
||||
template <size_t n>
|
||||
void PrintSol(const std::array<std::array<int, n>, n> &board) {
|
||||
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)) {
|
||||
inc_loop(i, 0, n - 1) {
|
||||
dec_loop(j, n - 1, 0) std::cout << Board[i][j] << " ";
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
bool CanIMove(int Board[n][n], int row, int col) {
|
||||
/**
|
||||
* Check if a queen can be placed on matrix
|
||||
* @tparam n number of matrix size
|
||||
* @param board matrix where numbers are saved
|
||||
* @param row current index in rows
|
||||
* @param col current index in columns
|
||||
* @returns `true` if queen can be placed on matrix
|
||||
* @returns `false` if queen can't be placed on matrix
|
||||
*/
|
||||
template <size_t n>
|
||||
bool CanIMove(const std::array<std::array<int, n>, n> &board, int row,
|
||||
int col) {
|
||||
/// check in the row
|
||||
inc_loop(i, 0, col - 1) {
|
||||
if (Board[row][i] == 1)
|
||||
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)
|
||||
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)
|
||||
if (board[i][j] == 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void NQueenSol(int Board[n][n], int col) {
|
||||
/**
|
||||
* Solve n queens problem
|
||||
* @tparam n number of matrix size
|
||||
* @param board matrix where numbers are saved
|
||||
* @param col current index in columns
|
||||
*/
|
||||
template <size_t n>
|
||||
void NQueenSol(std::array<std::array<int, n>, n> board, int col) {
|
||||
if (col >= n) {
|
||||
PrintSol(Board);
|
||||
PrintSol<n>(board);
|
||||
return;
|
||||
}
|
||||
inc_loop(i, 0, n - 1) {
|
||||
if (CanIMove(Board, i, col)) {
|
||||
Board[i][col] = 1;
|
||||
NQueenSol(Board, col + 1);
|
||||
Board[i][col] = 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace n_queens_optimized
|
||||
} // namespace backtracking
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
int Board[n][n] = {0};
|
||||
const int n = 4;
|
||||
std::array<std::array<int, n>, n> board{};
|
||||
|
||||
if (n % 2 == 0) {
|
||||
inc_loop(i, 0, n / 2 - 1) {
|
||||
if (CanIMove(Board, i, 0)) {
|
||||
Board[i][0] = 1;
|
||||
NQueenSol(Board, 1);
|
||||
Board[i][0] = 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 {
|
||||
inc_loop(i, 0, n / 2) {
|
||||
if (CanIMove(Board, i, 0)) {
|
||||
Board[i][0] = 1;
|
||||
NQueenSol(Board, 1);
|
||||
Board[i][0] = 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +1,25 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief pancake sort sorts a disordered stack of pancakes by flipping any number of pancakes using a spatula using minimum number of flips.
|
||||
* @brief pancake sort sorts a disordered stack of pancakes by flipping any
|
||||
* number of pancakes using a spatula using minimum number of flips.
|
||||
*
|
||||
* @details
|
||||
* Unlike a traditional sorting algorithm, which attempts to sort with the fewest comparisons possible,
|
||||
* the goal is to sort the sequence in as few reversals as possible.
|
||||
* Overall time complexity of pancake sort is O(n^2)
|
||||
* For example: example 1:-
|
||||
* Disordered pancake sizes: {2,5,3,7,8}
|
||||
* Sorted: {2,3,5,7,8}
|
||||
* For example: example 2:-
|
||||
* Disordered pancake sizes: {22,51,37,73,81}
|
||||
* Sorted: {22,37,51,73,81}
|
||||
* Unlike a traditional sorting algorithm, which attempts to sort with the
|
||||
* fewest comparisons possible, the goal is to sort the sequence in as few
|
||||
* reversals as possible. Overall time complexity of pancake sort is O(n^2) For
|
||||
* example: example 1:- Disordered pancake sizes: {2,5,3,7,8} Sorted:
|
||||
* {2,3,5,7,8} For example: example 2:- Disordered pancake sizes:
|
||||
* {22,51,37,73,81} Sorted: {22,37,51,73,81}
|
||||
* @author [Divyansh Gupta](https://github.com/divyansh12323)
|
||||
* @see more on [Pancake sort](https://en.wikipedia.org/wiki/Pancake_sorting)
|
||||
* @see related problem at [Leetcode](https://leetcode.com/problems/pancake-sorting/)
|
||||
* @see related problem at
|
||||
* [Leetcode](https://leetcode.com/problems/pancake-sorting/)
|
||||
*/
|
||||
|
||||
#include <iostream> // for io operations
|
||||
#include <vector> // for std::vector
|
||||
#include <algorithm> // for std::is_sorted
|
||||
#include <cassert> // for std::assert
|
||||
#include <iostream> // for io operations
|
||||
#include <vector> // for std::vector
|
||||
|
||||
/**
|
||||
* @namespace sorting
|
||||
@ -29,7 +28,8 @@
|
||||
namespace sorting {
|
||||
/**
|
||||
* @namespace pancake_sort
|
||||
* @brief Functions for [Pancake sort](https://en.wikipedia.org/wiki/Pancake_sorting) algorithm
|
||||
* @brief Functions for [Pancake
|
||||
* sort](https://en.wikipedia.org/wiki/Pancake_sorting) algorithm
|
||||
*/
|
||||
namespace pancake_sort {
|
||||
/**
|
||||
@ -51,7 +51,8 @@ namespace pancake_sort {
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief This implementation is for a C-style array input that gets modified in place.
|
||||
* @brief This implementation is for a C-style array input that gets modified in
|
||||
* place.
|
||||
* @param [start,end] arr our vector of elements.
|
||||
* @param size size of given array
|
||||
* @returns 0 on exit
|
||||
@ -59,7 +60,7 @@ namespace pancake_sort {
|
||||
template <typename T>
|
||||
int pancakeSort(std::vector<T> &arr, int size) {
|
||||
for (int i = size; i > 1; --i) {
|
||||
int max_index = 0, j; //intialize some variables.
|
||||
int max_index = 0, j = 0; // intialize some variables.
|
||||
T max_value = 0;
|
||||
for (j = 0; j < i; j++) {
|
||||
if (arr[j] >= max_value) {
|
||||
@ -98,7 +99,8 @@ static void test() {
|
||||
// example 2: vector of double
|
||||
const int size2 = 8;
|
||||
std::cout << "\nTest 2- as std::vector<double>...";
|
||||
std::vector<double> arr2 = {23.56, 10.62, 200.78, 111.484, 3.9, 1.2, 61.77, 79.6};
|
||||
std::vector<double> arr2 = {23.56, 10.62, 200.78, 111.484,
|
||||
3.9, 1.2, 61.77, 79.6};
|
||||
sorting::pancake_sort::pancakeSort(arr2, size2);
|
||||
assert(std::is_sorted(arr2.begin(), arr2.end()));
|
||||
std::cout << "Passed\n";
|
||||
|
Loading…
Reference in New Issue
Block a user