forked from skylarbpayne/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeautiful_n_game.cpp
More file actions
56 lines (51 loc) · 1.49 KB
/
Copy pathbeautiful_n_game.cpp
File metadata and controls
56 lines (51 loc) · 1.49 KB
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
/**
* Author: Skylar Payne
* Date: January 8, 2015
* Initially there is a number n written on the board. Two players start playing a game turn by turn.
* Each player replaces n with n - 2^k where k is a natural number and 2^k < n.
* The new number also has to have the same number of set bits in binary representation.
* If each player plays optimally, who wins given n?
**/
#include <iostream>
#include <unordered_map>
#include <vector>
#include <stdlib.h>
#include <assert.h>
bool p1_wins(int n, std::unordered_map<int, bool>& moves) {
if(moves.find(n) != moves.end()) {
return moves[n];
}
//find all valid moves.
//for each move, moves[n] = moves[n] || !p1_wins(move)
int msb_set;
for(msb_set = sizeof(int) * 8 - 1; ((n >> msb_set) & 1) == 0; --msb_set); // find msb set bit
std::vector<int> valid_moves;
for(int i = 0; i <= msb_set; ++i) {
if(((n >> i) & 1) == 0) {
valid_moves.push_back(i);
}
}
bool res = false;
for(int i = 0; i < valid_moves.size(); ++i) {
res = res || !p1_wins(n - (1 << valid_moves[i]), moves);
}
moves[n] = res;
return res;
}
bool is_p1_winner(int n) {
if(n == 0) {
return false;
}
std::unordered_map<int, bool> moves;
return p1_wins(n, moves);
}
int main(int argc, char** argv) {
if(argc != 2) {
std::cout << "Please provide n" << std::endl;
return -1;
}
//assert(is_p1_winner(3) == false);
//assert(is_p1_winner(5) == true);
std::cout << (is_p1_winner(atoi(argv[1])) ? "Player 1" : "Player 2") << " is the winner!" << std::endl;
return 0;
}