-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCan_I_Win.cpp
49 lines (40 loc) · 1.21 KB
/
Can_I_Win.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
class Solution {
public:
bool canIWin(int M, int T)
{
int sum = M*(M+1)/2; // sum of entire choosable pool
// I just pick 1 to win
if (T < 2) return true;
// Total is too large, nobody can win
else if (sum < T) return false;
// Total happens to match sum, whoever picks at odd times wins
else if (sum == T) return M%2;
// Non-trivial case: do DFS
// Initial total: T
// Initial game state: k = 0 (all numbers are not picked)
else return dfs(M, T, 0);
}
// DFS to check if I can win
// k: current game state
// T: remaining total to reach
bool dfs(int M, int T, int k)
{
// memorized
if (mem[k] != 0) return mem[k] > 0;
// total is already reached by opponent, so I lose
if (T <= 0) return false;
// try all currently available numbers
for (int i = 0; i < M; ++i)
// if (i+1) is available to pick and my opponent can't win after I picked, I win!
if (!(k&(1<<i)) && !dfs(M, T-i-1, k|(1<<i))) {
mem[k] = 1;
return true;
}
// Otherwise, I will lose
mem[k] = -1;
return false;
}
// m[key]: memorized game result when pool state = key
// 0: un-computed; 1: I win; -1: I lose
int mem[1<<20] = {};
};