Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c59c141

Browse files
Optimal Strategy for a Game
1 parent f2aff85 commit c59c141

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Amit Bansal - @amitbansal7 */
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
/*
6+
Optimal Strategy for a Game. Consider a row of n coins of values v(1) ... v(n), where n is even. We play
7+
a game against an opponent by alternating turns. In each turn, a player selects either the first or last
8+
coin from the row, removes it from the row permanently, and receives the value of the coin. Determine
9+
the maximum possible amount of money we can definitely win if we move first.
10+
*/
11+
int main()
12+
{
13+
int C[] = {20, 30, 2, 2, 2, 10};
14+
int c = sizeof(C) / sizeof(C[0]);
15+
16+
pair<int, int>DP[c][c];
17+
18+
for (int i = 0; i < c; i++)
19+
DP[i][i] = make_pair(C[i], 0);
20+
21+
for (int l = 2; l <= c; l++)
22+
{
23+
for (int i = 0; i < c - l + 1; i++)
24+
{
25+
int j = i + l - 1;
26+
27+
int a = C[i] + DP[i + 1][j].second;
28+
int b = C[j] + DP[i][j - 1].second;
29+
30+
if (a > b)
31+
DP[i][j] = make_pair(a, DP[i + 1][j].first);
32+
33+
else
34+
DP[i][j] = make_pair(b, DP[i][j - 1].first);
35+
}
36+
}
37+
printf("Player 1 gets : %d\nPlayer 2 gets : %d\n", DP[0][c - 1].first, DP[0][c - 1].second);
38+
39+
40+
return 0;
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /