-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_Nim_and_Grundy.cpp
77 lines (74 loc) · 1.61 KB
/
class_Nim_and_Grundy.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "auto_util_header.hpp"
class Nim {
private:
bool result;
public:
Nim(vll a) {
ll x = 0;
Loop(i, a.size()) x ^= a[i];
if (x != 0) result = true;
else result = false;
}
bool get_result() {
return result;
}
string get_winner(string player1, string player2) {
if (result) return player1;
else return player2;
}
};
class Grundy {
private:
vi grundies;
vi(*transition_rule)(int);
int get_grundy_val(int state) {
// memoization
if (grundies[state] == -1) {
set<int> s;
vi next_states = transition_rule(state);
for (const int next_state : next_states) {
s.insert(this->get_grundy_val(next_state));
}
int c = 0;
while (s.find(c) != s.end()) c++;
grundies[state] = c;
}
return grundies[state];
}
public:
Grundy(vi(*transition_rule)(int), int STATE_SIZE) {
// TODO: allocate code of states
this->grundies = vi(STATE_SIZE, -1);
this->transition_rule = transition_rule;
}
bool solve_game(const vi &states) {
// decide the grundy number in each states
vll xs(states.size());
Loop(i, states.size()) {
xs[i] = this->get_grundy_val(states[i]);
}
// return to Nim
Nim *nim = new Nim(xs);
bool result = nim->get_result();
delete nim;
return result;
}
string solve_game(const vi &states, string player0, string player1){
if (this->solve_game(states)) {
return player0;
}
else {
return player1;
}
}
};
vi transition_rule(int state) {
vi next_states;
{
// TODO: transition_rule
if (state > 0) {
next_states.push_back(state - 1);
}
}
return next_states;
}