-
Notifications
You must be signed in to change notification settings - Fork 0
/
nim.cpp
49 lines (42 loc) · 958 Bytes
/
nim.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
//
// nim.cpp
// xcode
//
// Created by Gokul Nadathur on 9/5/16.
// Copyright © 2016 Gokul Nadathur. All rights reserved.
//
#include <vector>
using namespace std;
class Solution {
public:
bool _canWinNim(int n, vector<int>& v, int player)
{
if (player == 0 && n <= 3) {
return true;
}
if (player == 1 && n <= 3) {
return false;
}
if (v[n-1] == -1) {
bool res = _canWinNim(n-1, v, !player) ||
_canWinNim(n-2, v, !player) ||
_canWinNim(n-3, v, !player);
v[n-1] = res ? 1 : 0;
}
return v[n-1];
}
bool __canWinNum(int n) {
return n % 4 != 0;
}
bool canWinNim(int n) {
if (n == 0) {
return false;
}
vector<int> v(n, -1);
return _canWinNim(n, v, 0);
}
};
int main(int argc, char** argv)
{
return 0;
}