#include <stdio.h>
#include <stdbool.h>
#include <string.h>
/*
Memoization arrays.
memo[x][y][z] stores the result of whether the player whose turn is given by z can win
from score x with y moves left.
*/
bool memo[31][201][2];
bool calculated[31][201][2]; // To check if already calculated
bool can_win(int current_score, int moves_left, bool is_alice_turn, int k) {
if (current_score == 0) {
return !is_alice_turn; // If it's Alice's turn, she loses, if it's Bob's turn, he loses
}
if (moves_left == 0) {
return false; // No moves left, no one wins
}
if (calculated[current_score][moves_left][is_alice_turn]) {
return memo[current_score][moves_left][is_alice_turn];
}
int possible_moves[6];
possible_moves[0] = (current_score + 1) % k;
possible_moves[1] = (current_score + 2) % k;
possible_moves[2] = (current_score + 3) % k;
possible_moves[3] = (current_score * 2) % k;
possible_moves[4] = (current_score * 3) % k;
possible_moves[5] = (current_score * current_score) % k;
if (is_alice_turn) {
for (int i = 0; i < 6; i++) {
if (can_win(possible_moves[i], moves_left - 1, !is_alice_turn, k)) {
memo[current_score][moves_left][is_alice_turn] = true;
calculated[current_score][moves_left][is_alice_turn] = true;
return true;
}
}
memo[current_score][moves_left][is_alice_turn] = false;
calculated[current_score][moves_left][is_alice_turn] = true;
return false;
} else {
for (int i = 0; i < 6; i++) {
if (!can_win(possible_moves[i], moves_left - 1, !is_alice_turn, k)) {
memo[current_score][moves_left][is_alice_turn] = false;
calculated[current_score][moves_left][is_alice_turn] = true;
return false;
}
}
memo[current_score][moves_left][is_alice_turn] = true;
calculated[current_score][moves_left][is_alice_turn] = true;
return true;
}
}
char *solve(int k, int n, int s) {
// Reset memoization arrays for each new game
memset(calculated, 0, sizeof(calculated));
if (can_win(s, n, true, k)) {
return "Alice";
} else {
return "Draw";
}
}
int main() {
int t, tc, k, n, s;
scanf("%d", &t);
for (tc = 0; tc < t; tc++) {
scanf("%d%d%d", &k, &n, &s);
printf("%sn", solve(k, n, s));
}
return 0;
}
im pretty sure that it has to do something with the “possible moves” but im not sure
For each game set, print either “Alice” or “Bob” (without quotes), indicating the winner of the game if both play optimally. If neither can win in N turns due to the other player moving optimally, print “Draw” (without quotes).
Sample Input 0
3
31 15 29
31 7 26
31 15 6
Sample Output 0
Alice
Draw
Draw
New contributor
rick scott is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.