-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathStone Game 4.cpp
38 lines (35 loc) · 907 Bytes
/
Stone Game 4.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
class Solution {
public:
int calc(int n, vector<int> &s, int p) {
// cout<<n<<" "<<p<<'\n';
if (s[n] != -1) return s[n] ^ p;
if (s[n - 1] == 0) {
s[n] = 1 ^ p;
return s[n];
}
int x = sqrt(n);
for (int i = x; i >= 1; i--) {
int a = calc(n - i * i, s, !p);
// cout<<n-i*i<<" "<<!p<<" a="<<a<<"\n";
if (a) {
s[n] = 1 ^ p;
return s[n] ^ p;
}
}
s[n] = 0 ^ p;
return s[n] ^ p;
}
bool winnerSquareGame(int n) {
vector<int> v(n + 1, -1);
v[0] = 0;
v[1] = 1;
// int a = calc(n, v, 0);
int a;
for (int i = 0; i <= n; i++) {
a = calc(i, v, 0);
// cout<<i<<" "<<v[i]<<"\n";
}
// return false;
return v[n] ? true : false;
}
};