You are on page 1of 5

T2 ASSIGNMENT

SUBJECT : C PROGRAMMING
MODULE -1
BATCH -12
TARGET-2
SEC-13
BRANCH-CSE
YEAR -1
REGISTRATION NO: 231FA04A32 –
Y.PUJITHA
231FA04A50 –
D.SIDDHARTHA
231FA04A25 – R.HARI
SURYA
PROBLEM STATEMENT:
Alice and Bob play a game with piles of stones. There are an even number of piles arranged in a
row, and each pile has a positive integer number of stones piles[i]. The objective of the game is to end
with the most stones. The total number of stones across all the piles is odd, so there are no ties. Alice
and Bob take turns, with Alice starting first. Each turn, a player takes the entire pile of stones either
from the beginning or from the end of the row. This continues until there are no more piles left, at
which point the person with the most stones wins. Assuming Alice and Bob play optimally, return
true if Alice wins the game, or false if Bob wins.

DESCRIPTION :

1. Initial Conditions:
 There are an even number of piles of stones.
 Each pile contains a positive integer number of stones.
 The total number of stones across all the piles is odd.
2. Turn-Based Play:
 Alice starts the game.
 Players take turns alternatively.
3. Move Rules:
 In each turn, a player can take the entire pile of stones from either the
beginning or the end of the row.
4. Objective:
 The objective of the game is not just to win but to end up with the most stones.
Since the total number of stones is odd, there can't be a tie.
5. Optimal Strategy:
 Alice and Bob will play optimally, meaning they will make the best possible
moves to maximize their own total stones and minimize their opponent's total
stones.
6. Determining the Winner:
 The game continues until there are no more piles left.
 The player with the most stones at the end wins.
To determine if Alice wins the game, we need to consider the following:
 If the total number of stones is already odd (which is given in the problem statement),
Alice can win by simply choosing any pile in her first move. This ensures that the
total number of stones she collects will be odd, and Bob will be left with an even
number of stones to collect. Since the total number of stones is odd, Alice will always
have more stones than Bob in the end.
 If the total number of stones is even, then Bob can win by using a strategy similar to
the one mentioned above. He starts by selecting any pile, and then he mirrors Alice's
moves throughout the game. This ensures that Bob always has an odd number of
stones, and Alice will be left with an even number of stones to collect. Bob will win
in this case.
So, in summary, if the total number of stones is initially odd, Alice wins; if it's initially even,
Bob wins. The key is that the player who starts the game can always force the other player
into a losing position by following the right strategy.

Algorithm:
1. Initialize two variables, Alice Score and bob Score, to keep track of the total
number of stones taken by Alice and Bob, respectively.
2. Use a loop to iterate through the piles of stones.
3. In each iteration, check if the current index is even or odd.
- If the index is even, it's Alice's turn. Add the number of stones in the current pile to
Alice Score.
- If the index is odd, it's Bob's turn. Add the number of stones in the current pile to bob
Score.
4. After the loop, compare Alice Score and bob Score to determine the winner.
- If Alice Score is greater than bob Score, return true (Alice wins). - Otherwise,
return false (Bob wins).

Source code:
#include <stdio.h>
#define MAX_PILES 100
int main() {
int piles[MAX_PILES];
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &piles[i]);
}
int dp[MAX_PILES][MAX_PILES];

// Initialize the DP array with the values when there's only one pile
for (int i = 0; i < n; i++) {
dp[i][i] = piles[i];
}

// Fill the DP array based on the optimal strategy


for (int length = 2; length <= n; length++) {
for (int i = 0; i <= n - length; i++) {
int j = i + length - 1;
int takeFirst = piles[i] - dp[i + 1][j];
int takeLast = piles[j] - dp[i][j - 1];
dp[i][j] = (takeFirst > takeLast) ? takeFirst : takeLast;
}
}

// If the difference between Alice and Bob's stone counts is positive, Alice wins
if (dp[0][n - 1] > 0) {
printf("true\n");
} else {
printf("false\n");
}

return 0;
}

Test code:
Input: piles = [5,3,4,5]
Output: true
Input :piles = [3,7,2,3]
Output: true

Output :

You might also like