-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame Theory Routine.cpp
62 lines (61 loc) · 1.7 KB
/
Game Theory Routine.cpp
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
60
61
62
//MinMax
bool dp[N];
int p, q, r, vis[N];
bool valid(int x){
return x >= 0; //check whether it is a possible move or not
}
bool MinMax(int n){
if(vis[n]) return dp[n];
vis[n]=1;
if(valid(n - 1) && !MinMax(n - 1)) return dp[n] = true;// n theke jesob valid move
if(valid(n - p) && !MinMax(n - p)) return dp[n] = true;// deoa jai segular jkono ekta
if(valid(n - q) && !MinMax(n - q)) return dp[n] = true;// false holei true return korbe
return dp[n] = false; //otherwise false
}
//Grundy
int dp[N];
int grundy(int x){
if(x <= 0) return 0;
int &ret = dp[x];
if(ret != -1) return ret;
set<int> st;
for(int i = 0; i < x; i++){
st.insert(grundy(i - 2) ^ grundy(x - i - 3));
//possible moves from current state
}
int cur = 0;
while(st.find(cur) != st.end()) cur += 1;
return ret = cur;
}
//Green Hackenbush
//value of dfs() > 0 then win
int dfs(int s, int p){
int g = 0;
for(int i = 0; i < adj[s].size(); i++){
int t = adj[s][i];
if(t == p) continue;
int x = dfs(t, s);
g ^= (x + 1);
}
return g;
}
//Red Blue Hackenbush
//res > 0 -> even win
//res < 0 -> odd win
//res == 0 don't play
// for multiple stalks add the value normally
ll ar[N];
ll hackenbush(int n){
ll V = 1LL << 50, lst = -1, res = 0;
int flag = 0;
for(int i = 0; i < n; i++){
ll cur = ar[i];
if(lst == -1) lst = cur;
if(cur % 2LL != lst % 2LL) flag = 1;
if(flag) V /= 2;
if(cur % 2LL) res -= V;
else res += V;
lst = cur;
}
return res;
}