TheAlgorithms-C-Plus-Plus/backtracking/minimax.cpp

64 lines
2.2 KiB
C++
Raw Normal View History

[fix/docs]: Update backtracking folder (#916) * [fix/docs]: Update backtracking/graph_coloring.cpp * Add CMakeLists.txt in backtracking folder * Add backtracking to CMakeLists.txt * fix: Fix build issues * docs: Various documentation fixes * fix: minimax.cpp issues * fix: sudoku_solve.cpp fixes * formatting source-code for 8ffbbb35cecc5fb3dac33957e14e9da50cff9469 * make he code neat and clean without global variables * fix 2 stars in comment * fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful * Update minimax.cpp * docs: minimax.cpp improvements * docs: Add Wikipedia link in minimax.cpp * fix: minimax.cpp vector fix * docs: fix Wikipedia link in minimax.cpp * docs: fix return statement in minimax.cpp * fix: sudoku_solve.cpp fixes * fix: more sudoku_solve.cpp fixes * fix: sudoku_solve.cpp fixes * fix: sudoku_solve.cpp * formatting source-code for 13b5b9b829eecc242746f6f00d54c35dad6c026a * docs: update graph_coloring.cpp description * fix: use array instead of vector (minimax.cpp) * feat: add namespace (minimax.cpp) * docs: update namespace description (graph_coloring.cpp) * fix: graph_coloring.cpp * fix: sudoku_solve.cpp fixes * fix: graph_coloring.cpp * fix: minimax.cpp * fix: more sudoku_solve.cpp fixes * fix: more graph_coloring.cpp fixes * fix: graph_coloring.cpp fixes * fix: sudoku_solve.cpp fixes * fix: minimax.cpp * fix: sudoku_solve.cpp fixes * fix: too few template arguments (std::array) * fix: too few template arguments (std::array, minimax.cpp) * fix: narrowing conversion from double to int (minimax.cpp) * fix: excess elements in struct initializer (graph_coloring.cpp) * fix: no matching function (graph_coloring.cpp) * fix: graph_coloring.cpp issues/errors * fix: knight_tour.cpp issues/errors * fix: sudoku_solve.cpp issues/errors * [fix/docs]: Various fixes in graph_coloring.cpp * fix: More graph_coloring.cpp fixes * docs: Add initial comment block (sudoku_solve.cpp) * fix: Add return statement (knight_tour.cpp) * fix: array fixes (graph_coloring.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (knight_tour.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (graph_coloring.cpp) * docs: Documentation improvements (graph_coloring.cpp) Thanks, @kvedala! * docs: Documentation improvements (sudoku_solve.cpp) * docs: Document function parameter (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Add long description (graph_coloring.cpp) * docs: Add long description (minimax.cpp) * docs: Add long description (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * docs: Documentation improvements (minimax.cpp) * docs: More documentation improvements (minimax.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * fix: sudoku_solve.cpp improvements Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-08 02:35:59 +08:00
/**
* @file
* @brief returns which is the longest/shortest number
* using [minimax](https://en.wikipedia.org/wiki/Minimax) algorithm
*
* @details
* Minimax (sometimes MinMax, MM or saddle point) is a decision rule used in
* artificial intelligence, decision theory, game theory, statistics,
* and philosophy for minimizing the possible loss for a worst case (maximum
* loss) scenario. When dealing with gains, it is referred to as "maximin"to
* maximize the minimum gain. Originally formulated for two-player zero-sum game
* theory, covering both the cases where players take alternate moves and those
* where they make simultaneous moves, it has also been extended to more complex
* games and to general decision-making in the presence of uncertainty.
*
[fix/docs]: Update backtracking folder (#916) * [fix/docs]: Update backtracking/graph_coloring.cpp * Add CMakeLists.txt in backtracking folder * Add backtracking to CMakeLists.txt * fix: Fix build issues * docs: Various documentation fixes * fix: minimax.cpp issues * fix: sudoku_solve.cpp fixes * formatting source-code for 8ffbbb35cecc5fb3dac33957e14e9da50cff9469 * make he code neat and clean without global variables * fix 2 stars in comment * fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful * Update minimax.cpp * docs: minimax.cpp improvements * docs: Add Wikipedia link in minimax.cpp * fix: minimax.cpp vector fix * docs: fix Wikipedia link in minimax.cpp * docs: fix return statement in minimax.cpp * fix: sudoku_solve.cpp fixes * fix: more sudoku_solve.cpp fixes * fix: sudoku_solve.cpp fixes * fix: sudoku_solve.cpp * formatting source-code for 13b5b9b829eecc242746f6f00d54c35dad6c026a * docs: update graph_coloring.cpp description * fix: use array instead of vector (minimax.cpp) * feat: add namespace (minimax.cpp) * docs: update namespace description (graph_coloring.cpp) * fix: graph_coloring.cpp * fix: sudoku_solve.cpp fixes * fix: graph_coloring.cpp * fix: minimax.cpp * fix: more sudoku_solve.cpp fixes * fix: more graph_coloring.cpp fixes * fix: graph_coloring.cpp fixes * fix: sudoku_solve.cpp fixes * fix: minimax.cpp * fix: sudoku_solve.cpp fixes * fix: too few template arguments (std::array) * fix: too few template arguments (std::array, minimax.cpp) * fix: narrowing conversion from double to int (minimax.cpp) * fix: excess elements in struct initializer (graph_coloring.cpp) * fix: no matching function (graph_coloring.cpp) * fix: graph_coloring.cpp issues/errors * fix: knight_tour.cpp issues/errors * fix: sudoku_solve.cpp issues/errors * [fix/docs]: Various fixes in graph_coloring.cpp * fix: More graph_coloring.cpp fixes * docs: Add initial comment block (sudoku_solve.cpp) * fix: Add return statement (knight_tour.cpp) * fix: array fixes (graph_coloring.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (knight_tour.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (graph_coloring.cpp) * docs: Documentation improvements (graph_coloring.cpp) Thanks, @kvedala! * docs: Documentation improvements (sudoku_solve.cpp) * docs: Document function parameter (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Add long description (graph_coloring.cpp) * docs: Add long description (minimax.cpp) * docs: Add long description (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * docs: Documentation improvements (minimax.cpp) * docs: More documentation improvements (minimax.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * fix: sudoku_solve.cpp improvements Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-08 02:35:59 +08:00
* @author [Gleison Batista](https://github.com/gleisonbs)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <algorithm> /// for std::max, std::min
#include <array> /// for std::array
#include <cmath> /// for log2
#include <iostream> /// for IO operations
/**
[fix/docs]: Update backtracking folder (#916) * [fix/docs]: Update backtracking/graph_coloring.cpp * Add CMakeLists.txt in backtracking folder * Add backtracking to CMakeLists.txt * fix: Fix build issues * docs: Various documentation fixes * fix: minimax.cpp issues * fix: sudoku_solve.cpp fixes * formatting source-code for 8ffbbb35cecc5fb3dac33957e14e9da50cff9469 * make he code neat and clean without global variables * fix 2 stars in comment * fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful * Update minimax.cpp * docs: minimax.cpp improvements * docs: Add Wikipedia link in minimax.cpp * fix: minimax.cpp vector fix * docs: fix Wikipedia link in minimax.cpp * docs: fix return statement in minimax.cpp * fix: sudoku_solve.cpp fixes * fix: more sudoku_solve.cpp fixes * fix: sudoku_solve.cpp fixes * fix: sudoku_solve.cpp * formatting source-code for 13b5b9b829eecc242746f6f00d54c35dad6c026a * docs: update graph_coloring.cpp description * fix: use array instead of vector (minimax.cpp) * feat: add namespace (minimax.cpp) * docs: update namespace description (graph_coloring.cpp) * fix: graph_coloring.cpp * fix: sudoku_solve.cpp fixes * fix: graph_coloring.cpp * fix: minimax.cpp * fix: more sudoku_solve.cpp fixes * fix: more graph_coloring.cpp fixes * fix: graph_coloring.cpp fixes * fix: sudoku_solve.cpp fixes * fix: minimax.cpp * fix: sudoku_solve.cpp fixes * fix: too few template arguments (std::array) * fix: too few template arguments (std::array, minimax.cpp) * fix: narrowing conversion from double to int (minimax.cpp) * fix: excess elements in struct initializer (graph_coloring.cpp) * fix: no matching function (graph_coloring.cpp) * fix: graph_coloring.cpp issues/errors * fix: knight_tour.cpp issues/errors * fix: sudoku_solve.cpp issues/errors * [fix/docs]: Various fixes in graph_coloring.cpp * fix: More graph_coloring.cpp fixes * docs: Add initial comment block (sudoku_solve.cpp) * fix: Add return statement (knight_tour.cpp) * fix: array fixes (graph_coloring.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (knight_tour.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (graph_coloring.cpp) * docs: Documentation improvements (graph_coloring.cpp) Thanks, @kvedala! * docs: Documentation improvements (sudoku_solve.cpp) * docs: Document function parameter (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Add long description (graph_coloring.cpp) * docs: Add long description (minimax.cpp) * docs: Add long description (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * docs: Documentation improvements (minimax.cpp) * docs: More documentation improvements (minimax.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * fix: sudoku_solve.cpp improvements Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-08 02:35:59 +08:00
* @namespace backtracking
* @brief Backtracking algorithms
*/
namespace backtracking {
/**
* @brief Check which is the maximum/minimum number in the array
[fix/docs]: Update backtracking folder (#916) * [fix/docs]: Update backtracking/graph_coloring.cpp * Add CMakeLists.txt in backtracking folder * Add backtracking to CMakeLists.txt * fix: Fix build issues * docs: Various documentation fixes * fix: minimax.cpp issues * fix: sudoku_solve.cpp fixes * formatting source-code for 8ffbbb35cecc5fb3dac33957e14e9da50cff9469 * make he code neat and clean without global variables * fix 2 stars in comment * fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful * Update minimax.cpp * docs: minimax.cpp improvements * docs: Add Wikipedia link in minimax.cpp * fix: minimax.cpp vector fix * docs: fix Wikipedia link in minimax.cpp * docs: fix return statement in minimax.cpp * fix: sudoku_solve.cpp fixes * fix: more sudoku_solve.cpp fixes * fix: sudoku_solve.cpp fixes * fix: sudoku_solve.cpp * formatting source-code for 13b5b9b829eecc242746f6f00d54c35dad6c026a * docs: update graph_coloring.cpp description * fix: use array instead of vector (minimax.cpp) * feat: add namespace (minimax.cpp) * docs: update namespace description (graph_coloring.cpp) * fix: graph_coloring.cpp * fix: sudoku_solve.cpp fixes * fix: graph_coloring.cpp * fix: minimax.cpp * fix: more sudoku_solve.cpp fixes * fix: more graph_coloring.cpp fixes * fix: graph_coloring.cpp fixes * fix: sudoku_solve.cpp fixes * fix: minimax.cpp * fix: sudoku_solve.cpp fixes * fix: too few template arguments (std::array) * fix: too few template arguments (std::array, minimax.cpp) * fix: narrowing conversion from double to int (minimax.cpp) * fix: excess elements in struct initializer (graph_coloring.cpp) * fix: no matching function (graph_coloring.cpp) * fix: graph_coloring.cpp issues/errors * fix: knight_tour.cpp issues/errors * fix: sudoku_solve.cpp issues/errors * [fix/docs]: Various fixes in graph_coloring.cpp * fix: More graph_coloring.cpp fixes * docs: Add initial comment block (sudoku_solve.cpp) * fix: Add return statement (knight_tour.cpp) * fix: array fixes (graph_coloring.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (knight_tour.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (graph_coloring.cpp) * docs: Documentation improvements (graph_coloring.cpp) Thanks, @kvedala! * docs: Documentation improvements (sudoku_solve.cpp) * docs: Document function parameter (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Add long description (graph_coloring.cpp) * docs: Add long description (minimax.cpp) * docs: Add long description (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * docs: Documentation improvements (minimax.cpp) * docs: More documentation improvements (minimax.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * fix: sudoku_solve.cpp improvements Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-08 02:35:59 +08:00
* @param depth current depth in game tree
* @param node_index current index in array
* @param is_max if current index is the longest number
* @param scores saved numbers in array
* @param height maximum height for game tree
* @returns the maximum or minimum number
[fix/docs]: Update backtracking folder (#916) * [fix/docs]: Update backtracking/graph_coloring.cpp * Add CMakeLists.txt in backtracking folder * Add backtracking to CMakeLists.txt * fix: Fix build issues * docs: Various documentation fixes * fix: minimax.cpp issues * fix: sudoku_solve.cpp fixes * formatting source-code for 8ffbbb35cecc5fb3dac33957e14e9da50cff9469 * make he code neat and clean without global variables * fix 2 stars in comment * fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful * Update minimax.cpp * docs: minimax.cpp improvements * docs: Add Wikipedia link in minimax.cpp * fix: minimax.cpp vector fix * docs: fix Wikipedia link in minimax.cpp * docs: fix return statement in minimax.cpp * fix: sudoku_solve.cpp fixes * fix: more sudoku_solve.cpp fixes * fix: sudoku_solve.cpp fixes * fix: sudoku_solve.cpp * formatting source-code for 13b5b9b829eecc242746f6f00d54c35dad6c026a * docs: update graph_coloring.cpp description * fix: use array instead of vector (minimax.cpp) * feat: add namespace (minimax.cpp) * docs: update namespace description (graph_coloring.cpp) * fix: graph_coloring.cpp * fix: sudoku_solve.cpp fixes * fix: graph_coloring.cpp * fix: minimax.cpp * fix: more sudoku_solve.cpp fixes * fix: more graph_coloring.cpp fixes * fix: graph_coloring.cpp fixes * fix: sudoku_solve.cpp fixes * fix: minimax.cpp * fix: sudoku_solve.cpp fixes * fix: too few template arguments (std::array) * fix: too few template arguments (std::array, minimax.cpp) * fix: narrowing conversion from double to int (minimax.cpp) * fix: excess elements in struct initializer (graph_coloring.cpp) * fix: no matching function (graph_coloring.cpp) * fix: graph_coloring.cpp issues/errors * fix: knight_tour.cpp issues/errors * fix: sudoku_solve.cpp issues/errors * [fix/docs]: Various fixes in graph_coloring.cpp * fix: More graph_coloring.cpp fixes * docs: Add initial comment block (sudoku_solve.cpp) * fix: Add return statement (knight_tour.cpp) * fix: array fixes (graph_coloring.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (knight_tour.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (graph_coloring.cpp) * docs: Documentation improvements (graph_coloring.cpp) Thanks, @kvedala! * docs: Documentation improvements (sudoku_solve.cpp) * docs: Document function parameter (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Add long description (graph_coloring.cpp) * docs: Add long description (minimax.cpp) * docs: Add long description (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * docs: Documentation improvements (minimax.cpp) * docs: More documentation improvements (minimax.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * fix: sudoku_solve.cpp improvements Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-08 02:35:59 +08:00
*/
template <size_t T>
int minimax(int depth, int node_index, bool is_max,
const std::array<int, T> &scores, double height) {
if (depth == height) {
return scores[node_index];
[fix/docs]: Update backtracking folder (#916) * [fix/docs]: Update backtracking/graph_coloring.cpp * Add CMakeLists.txt in backtracking folder * Add backtracking to CMakeLists.txt * fix: Fix build issues * docs: Various documentation fixes * fix: minimax.cpp issues * fix: sudoku_solve.cpp fixes * formatting source-code for 8ffbbb35cecc5fb3dac33957e14e9da50cff9469 * make he code neat and clean without global variables * fix 2 stars in comment * fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful * Update minimax.cpp * docs: minimax.cpp improvements * docs: Add Wikipedia link in minimax.cpp * fix: minimax.cpp vector fix * docs: fix Wikipedia link in minimax.cpp * docs: fix return statement in minimax.cpp * fix: sudoku_solve.cpp fixes * fix: more sudoku_solve.cpp fixes * fix: sudoku_solve.cpp fixes * fix: sudoku_solve.cpp * formatting source-code for 13b5b9b829eecc242746f6f00d54c35dad6c026a * docs: update graph_coloring.cpp description * fix: use array instead of vector (minimax.cpp) * feat: add namespace (minimax.cpp) * docs: update namespace description (graph_coloring.cpp) * fix: graph_coloring.cpp * fix: sudoku_solve.cpp fixes * fix: graph_coloring.cpp * fix: minimax.cpp * fix: more sudoku_solve.cpp fixes * fix: more graph_coloring.cpp fixes * fix: graph_coloring.cpp fixes * fix: sudoku_solve.cpp fixes * fix: minimax.cpp * fix: sudoku_solve.cpp fixes * fix: too few template arguments (std::array) * fix: too few template arguments (std::array, minimax.cpp) * fix: narrowing conversion from double to int (minimax.cpp) * fix: excess elements in struct initializer (graph_coloring.cpp) * fix: no matching function (graph_coloring.cpp) * fix: graph_coloring.cpp issues/errors * fix: knight_tour.cpp issues/errors * fix: sudoku_solve.cpp issues/errors * [fix/docs]: Various fixes in graph_coloring.cpp * fix: More graph_coloring.cpp fixes * docs: Add initial comment block (sudoku_solve.cpp) * fix: Add return statement (knight_tour.cpp) * fix: array fixes (graph_coloring.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (knight_tour.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (graph_coloring.cpp) * docs: Documentation improvements (graph_coloring.cpp) Thanks, @kvedala! * docs: Documentation improvements (sudoku_solve.cpp) * docs: Document function parameter (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Add long description (graph_coloring.cpp) * docs: Add long description (minimax.cpp) * docs: Add long description (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * docs: Documentation improvements (minimax.cpp) * docs: More documentation improvements (minimax.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * fix: sudoku_solve.cpp improvements Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-08 02:35:59 +08:00
}
int v1 = minimax(depth + 1, node_index * 2, !is_max, scores, height);
int v2 = minimax(depth + 1, node_index * 2 + 1, !is_max, scores, height);
[fix/docs]: Update backtracking folder (#916) * [fix/docs]: Update backtracking/graph_coloring.cpp * Add CMakeLists.txt in backtracking folder * Add backtracking to CMakeLists.txt * fix: Fix build issues * docs: Various documentation fixes * fix: minimax.cpp issues * fix: sudoku_solve.cpp fixes * formatting source-code for 8ffbbb35cecc5fb3dac33957e14e9da50cff9469 * make he code neat and clean without global variables * fix 2 stars in comment * fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful * Update minimax.cpp * docs: minimax.cpp improvements * docs: Add Wikipedia link in minimax.cpp * fix: minimax.cpp vector fix * docs: fix Wikipedia link in minimax.cpp * docs: fix return statement in minimax.cpp * fix: sudoku_solve.cpp fixes * fix: more sudoku_solve.cpp fixes * fix: sudoku_solve.cpp fixes * fix: sudoku_solve.cpp * formatting source-code for 13b5b9b829eecc242746f6f00d54c35dad6c026a * docs: update graph_coloring.cpp description * fix: use array instead of vector (minimax.cpp) * feat: add namespace (minimax.cpp) * docs: update namespace description (graph_coloring.cpp) * fix: graph_coloring.cpp * fix: sudoku_solve.cpp fixes * fix: graph_coloring.cpp * fix: minimax.cpp * fix: more sudoku_solve.cpp fixes * fix: more graph_coloring.cpp fixes * fix: graph_coloring.cpp fixes * fix: sudoku_solve.cpp fixes * fix: minimax.cpp * fix: sudoku_solve.cpp fixes * fix: too few template arguments (std::array) * fix: too few template arguments (std::array, minimax.cpp) * fix: narrowing conversion from double to int (minimax.cpp) * fix: excess elements in struct initializer (graph_coloring.cpp) * fix: no matching function (graph_coloring.cpp) * fix: graph_coloring.cpp issues/errors * fix: knight_tour.cpp issues/errors * fix: sudoku_solve.cpp issues/errors * [fix/docs]: Various fixes in graph_coloring.cpp * fix: More graph_coloring.cpp fixes * docs: Add initial comment block (sudoku_solve.cpp) * fix: Add return statement (knight_tour.cpp) * fix: array fixes (graph_coloring.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (knight_tour.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (graph_coloring.cpp) * docs: Documentation improvements (graph_coloring.cpp) Thanks, @kvedala! * docs: Documentation improvements (sudoku_solve.cpp) * docs: Document function parameter (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Add long description (graph_coloring.cpp) * docs: Add long description (minimax.cpp) * docs: Add long description (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * docs: Documentation improvements (minimax.cpp) * docs: More documentation improvements (minimax.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * fix: sudoku_solve.cpp improvements Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-08 02:35:59 +08:00
return is_max ? std::max(v1, v2) : std::min(v1, v2);
}
} // namespace backtracking
[fix/docs]: Update backtracking folder (#916) * [fix/docs]: Update backtracking/graph_coloring.cpp * Add CMakeLists.txt in backtracking folder * Add backtracking to CMakeLists.txt * fix: Fix build issues * docs: Various documentation fixes * fix: minimax.cpp issues * fix: sudoku_solve.cpp fixes * formatting source-code for 8ffbbb35cecc5fb3dac33957e14e9da50cff9469 * make he code neat and clean without global variables * fix 2 stars in comment * fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful * Update minimax.cpp * docs: minimax.cpp improvements * docs: Add Wikipedia link in minimax.cpp * fix: minimax.cpp vector fix * docs: fix Wikipedia link in minimax.cpp * docs: fix return statement in minimax.cpp * fix: sudoku_solve.cpp fixes * fix: more sudoku_solve.cpp fixes * fix: sudoku_solve.cpp fixes * fix: sudoku_solve.cpp * formatting source-code for 13b5b9b829eecc242746f6f00d54c35dad6c026a * docs: update graph_coloring.cpp description * fix: use array instead of vector (minimax.cpp) * feat: add namespace (minimax.cpp) * docs: update namespace description (graph_coloring.cpp) * fix: graph_coloring.cpp * fix: sudoku_solve.cpp fixes * fix: graph_coloring.cpp * fix: minimax.cpp * fix: more sudoku_solve.cpp fixes * fix: more graph_coloring.cpp fixes * fix: graph_coloring.cpp fixes * fix: sudoku_solve.cpp fixes * fix: minimax.cpp * fix: sudoku_solve.cpp fixes * fix: too few template arguments (std::array) * fix: too few template arguments (std::array, minimax.cpp) * fix: narrowing conversion from double to int (minimax.cpp) * fix: excess elements in struct initializer (graph_coloring.cpp) * fix: no matching function (graph_coloring.cpp) * fix: graph_coloring.cpp issues/errors * fix: knight_tour.cpp issues/errors * fix: sudoku_solve.cpp issues/errors * [fix/docs]: Various fixes in graph_coloring.cpp * fix: More graph_coloring.cpp fixes * docs: Add initial comment block (sudoku_solve.cpp) * fix: Add return statement (knight_tour.cpp) * fix: array fixes (graph_coloring.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (knight_tour.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (graph_coloring.cpp) * docs: Documentation improvements (graph_coloring.cpp) Thanks, @kvedala! * docs: Documentation improvements (sudoku_solve.cpp) * docs: Document function parameter (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Add long description (graph_coloring.cpp) * docs: Add long description (minimax.cpp) * docs: Add long description (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * docs: Documentation improvements (minimax.cpp) * docs: More documentation improvements (minimax.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * fix: sudoku_solve.cpp improvements Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-08 02:35:59 +08:00
/**
* @brief Main function
* @returns 0 on exit
[fix/docs]: Update backtracking folder (#916) * [fix/docs]: Update backtracking/graph_coloring.cpp * Add CMakeLists.txt in backtracking folder * Add backtracking to CMakeLists.txt * fix: Fix build issues * docs: Various documentation fixes * fix: minimax.cpp issues * fix: sudoku_solve.cpp fixes * formatting source-code for 8ffbbb35cecc5fb3dac33957e14e9da50cff9469 * make he code neat and clean without global variables * fix 2 stars in comment * fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful * Update minimax.cpp * docs: minimax.cpp improvements * docs: Add Wikipedia link in minimax.cpp * fix: minimax.cpp vector fix * docs: fix Wikipedia link in minimax.cpp * docs: fix return statement in minimax.cpp * fix: sudoku_solve.cpp fixes * fix: more sudoku_solve.cpp fixes * fix: sudoku_solve.cpp fixes * fix: sudoku_solve.cpp * formatting source-code for 13b5b9b829eecc242746f6f00d54c35dad6c026a * docs: update graph_coloring.cpp description * fix: use array instead of vector (minimax.cpp) * feat: add namespace (minimax.cpp) * docs: update namespace description (graph_coloring.cpp) * fix: graph_coloring.cpp * fix: sudoku_solve.cpp fixes * fix: graph_coloring.cpp * fix: minimax.cpp * fix: more sudoku_solve.cpp fixes * fix: more graph_coloring.cpp fixes * fix: graph_coloring.cpp fixes * fix: sudoku_solve.cpp fixes * fix: minimax.cpp * fix: sudoku_solve.cpp fixes * fix: too few template arguments (std::array) * fix: too few template arguments (std::array, minimax.cpp) * fix: narrowing conversion from double to int (minimax.cpp) * fix: excess elements in struct initializer (graph_coloring.cpp) * fix: no matching function (graph_coloring.cpp) * fix: graph_coloring.cpp issues/errors * fix: knight_tour.cpp issues/errors * fix: sudoku_solve.cpp issues/errors * [fix/docs]: Various fixes in graph_coloring.cpp * fix: More graph_coloring.cpp fixes * docs: Add initial comment block (sudoku_solve.cpp) * fix: Add return statement (knight_tour.cpp) * fix: array fixes (graph_coloring.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (knight_tour.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (graph_coloring.cpp) * docs: Documentation improvements (graph_coloring.cpp) Thanks, @kvedala! * docs: Documentation improvements (sudoku_solve.cpp) * docs: Document function parameter (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Add long description (graph_coloring.cpp) * docs: Add long description (minimax.cpp) * docs: Add long description (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * docs: Documentation improvements (minimax.cpp) * docs: More documentation improvements (minimax.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * fix: sudoku_solve.cpp improvements Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-08 02:35:59 +08:00
*/
int main() {
[fix/docs]: Update backtracking folder (#916) * [fix/docs]: Update backtracking/graph_coloring.cpp * Add CMakeLists.txt in backtracking folder * Add backtracking to CMakeLists.txt * fix: Fix build issues * docs: Various documentation fixes * fix: minimax.cpp issues * fix: sudoku_solve.cpp fixes * formatting source-code for 8ffbbb35cecc5fb3dac33957e14e9da50cff9469 * make he code neat and clean without global variables * fix 2 stars in comment * fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful * Update minimax.cpp * docs: minimax.cpp improvements * docs: Add Wikipedia link in minimax.cpp * fix: minimax.cpp vector fix * docs: fix Wikipedia link in minimax.cpp * docs: fix return statement in minimax.cpp * fix: sudoku_solve.cpp fixes * fix: more sudoku_solve.cpp fixes * fix: sudoku_solve.cpp fixes * fix: sudoku_solve.cpp * formatting source-code for 13b5b9b829eecc242746f6f00d54c35dad6c026a * docs: update graph_coloring.cpp description * fix: use array instead of vector (minimax.cpp) * feat: add namespace (minimax.cpp) * docs: update namespace description (graph_coloring.cpp) * fix: graph_coloring.cpp * fix: sudoku_solve.cpp fixes * fix: graph_coloring.cpp * fix: minimax.cpp * fix: more sudoku_solve.cpp fixes * fix: more graph_coloring.cpp fixes * fix: graph_coloring.cpp fixes * fix: sudoku_solve.cpp fixes * fix: minimax.cpp * fix: sudoku_solve.cpp fixes * fix: too few template arguments (std::array) * fix: too few template arguments (std::array, minimax.cpp) * fix: narrowing conversion from double to int (minimax.cpp) * fix: excess elements in struct initializer (graph_coloring.cpp) * fix: no matching function (graph_coloring.cpp) * fix: graph_coloring.cpp issues/errors * fix: knight_tour.cpp issues/errors * fix: sudoku_solve.cpp issues/errors * [fix/docs]: Various fixes in graph_coloring.cpp * fix: More graph_coloring.cpp fixes * docs: Add initial comment block (sudoku_solve.cpp) * fix: Add return statement (knight_tour.cpp) * fix: array fixes (graph_coloring.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (knight_tour.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (graph_coloring.cpp) * docs: Documentation improvements (graph_coloring.cpp) Thanks, @kvedala! * docs: Documentation improvements (sudoku_solve.cpp) * docs: Document function parameter (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Add long description (graph_coloring.cpp) * docs: Add long description (minimax.cpp) * docs: Add long description (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * docs: Documentation improvements (minimax.cpp) * docs: More documentation improvements (minimax.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * fix: sudoku_solve.cpp improvements Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-08 02:35:59 +08:00
std::array<int, 8> scores = {90, 23, 6, 33, 21, 65, 123, 34423};
double height = log2(scores.size());
std::cout << "Optimal value: "
<< backtracking::minimax(0, 0, true, scores, height) << std::endl;
[fix/docs]: Update backtracking folder (#916) * [fix/docs]: Update backtracking/graph_coloring.cpp * Add CMakeLists.txt in backtracking folder * Add backtracking to CMakeLists.txt * fix: Fix build issues * docs: Various documentation fixes * fix: minimax.cpp issues * fix: sudoku_solve.cpp fixes * formatting source-code for 8ffbbb35cecc5fb3dac33957e14e9da50cff9469 * make he code neat and clean without global variables * fix 2 stars in comment * fix MSVC errors by forcing template parameter in function calls Note: This is identical to passing it as a function parameter, and may not be helpful * Update minimax.cpp * docs: minimax.cpp improvements * docs: Add Wikipedia link in minimax.cpp * fix: minimax.cpp vector fix * docs: fix Wikipedia link in minimax.cpp * docs: fix return statement in minimax.cpp * fix: sudoku_solve.cpp fixes * fix: more sudoku_solve.cpp fixes * fix: sudoku_solve.cpp fixes * fix: sudoku_solve.cpp * formatting source-code for 13b5b9b829eecc242746f6f00d54c35dad6c026a * docs: update graph_coloring.cpp description * fix: use array instead of vector (minimax.cpp) * feat: add namespace (minimax.cpp) * docs: update namespace description (graph_coloring.cpp) * fix: graph_coloring.cpp * fix: sudoku_solve.cpp fixes * fix: graph_coloring.cpp * fix: minimax.cpp * fix: more sudoku_solve.cpp fixes * fix: more graph_coloring.cpp fixes * fix: graph_coloring.cpp fixes * fix: sudoku_solve.cpp fixes * fix: minimax.cpp * fix: sudoku_solve.cpp fixes * fix: too few template arguments (std::array) * fix: too few template arguments (std::array, minimax.cpp) * fix: narrowing conversion from double to int (minimax.cpp) * fix: excess elements in struct initializer (graph_coloring.cpp) * fix: no matching function (graph_coloring.cpp) * fix: graph_coloring.cpp issues/errors * fix: knight_tour.cpp issues/errors * fix: sudoku_solve.cpp issues/errors * [fix/docs]: Various fixes in graph_coloring.cpp * fix: More graph_coloring.cpp fixes * docs: Add initial comment block (sudoku_solve.cpp) * fix: Add return statement (knight_tour.cpp) * fix: array fixes (graph_coloring.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (knight_tour.cpp) * docs: documentation improvements (sudoku_solve.cpp) * docs: documentation improvements (graph_coloring.cpp) * docs: Documentation improvements (graph_coloring.cpp) Thanks, @kvedala! * docs: Documentation improvements (sudoku_solve.cpp) * docs: Document function parameter (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Add long description (graph_coloring.cpp) * docs: Add long description (minimax.cpp) * docs: Add long description (sudoku_solve.cpp) * docs: Documentation improvements (knight_tour.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * docs: Documentation improvements (minimax.cpp) * docs: More documentation improvements (minimax.cpp) * docs: Documentation improvements (sudoku_solve.cpp) * fix: sudoku_solve.cpp improvements Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-08 02:35:59 +08:00
return 0;
}