2019-12-07 15:21:23 +08:00
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
|
|
|
using std::max;
|
|
|
|
using std::min;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
int minimax(int depth, int node_index, bool is_max, vector<int> scores,
|
2020-05-30 07:26:30 +08:00
|
|
|
int height)
|
|
|
|
{
|
2019-12-07 15:21:23 +08:00
|
|
|
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 ? max(v1, v2) : min(v1, v2);
|
|
|
|
}
|
|
|
|
|
2020-05-30 07:26:30 +08:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
vector<int> scores = {90, 23, 6, 33, 21, 65, 123, 34423};
|
2019-12-07 15:21:23 +08:00
|
|
|
int height = log2(scores.size());
|
|
|
|
|
|
|
|
cout << "Optimal value: " << minimax(0, 0, true, scores, height) << endl;
|
|
|
|
}
|