-
Notifications
You must be signed in to change notification settings - Fork 13
/
minimax-exercise.c
59 lines (51 loc) · 1.42 KB
/
minimax-exercise.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#define MIN(a,b) ((a<b)?a:b)
#define MAX(a,b) ((a<b)?b:a)
typedef struct _Node {
int data;
bool leaf;
} Node;
Node createNode(int p_data, bool p_bool) { return (Node) { p_data, p_bool }; }
int D, B, vis = 1;
Node arr[10000];
int minimax ( int index, int a, int b, bool max ) {
int val = max ? INT_MIN : INT_MAX;
Node *p = &arr[index];
if(p->leaf) { return p->data; }
if(max)
for (int i=1; i <= B; i++) {
vis++;
int tmp = minimax(B*index+i, a, b, false);
val = MAX(val, tmp);
a = MAX(a , val);
if(a>=b) break;
}
else
for (int i=1; i <= B; i++) {
vis++;
int tmp = minimax(B*index+i, a, b, true);
val = MIN(val, tmp);
b = MIN(b , val);
if(a>=b) break;
}
return val;
}
main() {
scanf("%d%d", &D, &B); fgetc(stdin);
char l[40001], *p; fgets(l, 40001, stdin);
int cmpt = 0;
for( int i = 0; i < D; i++ )
for( int j = 0; j < pow(B,i); j++, cmpt++ )
arr[cmpt] = createNode(0, false);
p = strtok(l, " ");
while(p != NULL) {
arr[cmpt] = createNode(atoi(p), true);
cmpt++;
p = strtok(NULL, " ");
}
printf("%d ", minimax(0, INT_MIN, INT_MAX, true)); printf("%d\n", vis);
}