TheAlgorithms-C-Plus-Plus/backtracking/minimax.cpp
David Leal 25b39a34fa
[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 8ffbbb35ce

* 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 13b5b9b829

* 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-07 14:35:59 -04:00

62 lines
2.1 KiB
C++

/**
* @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.
*
* @author [Gleison Batista](https://github.com/gleisonbs)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <algorithm>
#include <cmath>
#include <iostream>
#include <array>
/**
* @namespace backtracking
* @brief Backtracking algorithms
*/
namespace backtracking {
/**
* Check which number is the maximum/minimum in the array
* @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
* @return maximum or minimum number
*/
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];
}
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);
return is_max ? std::max(v1, v2) : std::min(v1, v2);
}
} // namespace backtracking
/**
* Main function
*/
int main() {
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;
return 0;
}