mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-14 02:30:40 +08:00
[feat/fix/docs]: Improvements in the backtracking folder (#1553)
* [feat/fix/docs]: Improvements in the...
...`backtracking` folder, and minor fixes in the `others/iterative_tree_traversals.cpp` and the `math/check_prime.cpp` files.
* clang-format and clang-tidy fixes for 9cc3951d
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com>
This commit is contained in:
@@ -6,33 +6,34 @@
|
||||
* @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.
|
||||
*
|
||||
* 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>
|
||||
#include <algorithm> /// for std::max, std::min
|
||||
#include <array> /// for std::array
|
||||
#include <cmath> /// for log2
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
/**
|
||||
* @namespace backtracking
|
||||
* @brief Backtracking algorithms
|
||||
*/
|
||||
namespace backtracking {
|
||||
/**
|
||||
* Check which number is the maximum/minimum in the array
|
||||
* @brief Check which is the maximum/minimum number 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
|
||||
* @returns the maximum or minimum number
|
||||
*/
|
||||
template <size_t T>
|
||||
int minimax(int depth, int node_index, bool is_max,
|
||||
@@ -46,16 +47,17 @@ int minimax(int depth, int node_index, bool is_max,
|
||||
|
||||
return is_max ? std::max(v1, v2) : std::min(v1, v2);
|
||||
}
|
||||
} // namespace backtracking
|
||||
} // namespace backtracking
|
||||
|
||||
/**
|
||||
* Main function
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
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;
|
||||
std::cout << "Optimal value: "
|
||||
<< backtracking::minimax(0, 0, true, scores, height) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user