TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
minimax.cpp
Go to the documentation of this file.
1
19
#include <algorithm>
20
#include <array>
21
#include <cmath>
22
#include <iostream>
23
28
namespace
backtracking
{
38
template
<
size_t
T>
39
int
minimax
(
int
depth,
int
node_index,
bool
is_max,
40
const
std::array<int, T> &scores,
double
height
) {
41
if
(depth ==
height
) {
42
return
scores[node_index];
43
}
44
45
int
v1 =
minimax
(depth + 1, node_index * 2, !is_max, scores,
height
);
46
int
v2 =
minimax
(depth + 1, node_index * 2 + 1, !is_max, scores,
height
);
47
48
return
is_max ? std::max(v1, v2) : std::min(v1, v2);
49
}
50
}
// namespace backtracking
51
56
int
main
() {
57
std::array<int, 8> scores = {90, 23, 6, 33, 21, 65, 123, 34423};
58
double
height
= log2(scores.size());
59
60
std::cout <<
"Optimal value: "
61
<<
backtracking::minimax
(0, 0,
true
, scores,
height
) << std::endl;
62
return
0;
63
}
height
int height(node *root)
Definition
avltree.cpp:38
main
int main()
Main function.
Definition
minimax.cpp:56
backtracking
for vector container
backtracking::minimax
int minimax(int depth, int node_index, bool is_max, const std::array< int, T > &scores, double height)
Check which is the maximum/minimum number in the array.
Definition
minimax.cpp:39
backtracking
minimax.cpp
Generated by
1.12.0