Created
August 2, 2018 15:16
-
-
Save gfredtech/885ba937398303e22cee3a8eadf9dbbc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <cstring> | |
#include <map> | |
#define NUMBER_OF_PLAYERS 3 | |
#define MAX_NUMBER_OF_STONES 500 | |
using namespace std; | |
int dp[NUMBER_OF_PLAYERS][MAX_NUMBER_OF_STONES]; | |
map<int, string> names; | |
int stone_game(int turn, int stonesLeft) { | |
if (stonesLeft < 0) return -1; | |
if (stonesLeft < 3) return (turn + 2) % 3; | |
if (dp[turn][stonesLeft] != -1) | |
return dp[turn][stonesLeft]; | |
int &result = dp[turn][stonesLeft]; | |
int choice1, choice2, choice3; | |
choice1 = stone_game(turn + 1, stonesLeft - 3); | |
choice2 = stone_game(turn + 1, stonesLeft - 5); | |
choice3 = stone_game(turn + 1, stonesLeft - 7); | |
if (choice1 == turn) | |
return result = turn; | |
if (choice2 == turn) | |
return result = turn; | |
if (choice3 == turn) | |
return result = turn; | |
return result = max(max(choice2, choice1), choice3); | |
} | |
int main() { | |
memset(dp, -1, sizeof dp); | |
names.insert(make_pair(0, "Ama")); | |
names.insert(make_pair(1, "Adjoa")); | |
names.insert(make_pair(2, "Yaa")); | |
int t; | |
cout << "Enter the number of rounds: "; | |
cin >> t; | |
for (int i = 1; i <= t; i++) { | |
int n; | |
cout << "Enter the number of stones: "; | |
cin >> n; | |
int winner = stone_game(0, n); | |
auto it = names.begin(); | |
while (it != names.end()) { | |
if (it->first == winner) { | |
cout << it->second << " wins in round " << i << endl; | |
break; | |
} | |
it++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment