-
Notifications
You must be signed in to change notification settings - Fork 59
/
Optimal Game Strategy DP.cpp
51 lines (43 loc) · 1.25 KB
/
Optimal Game Strategy DP.cpp
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
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
// Given an array , two players play the game of picking numbers alternatively from either ends. Find the max Score one can pick.
int arr[100000];
int dp[1000][1000];
int playOptimally(int n){
int ans;
int table[n][n], gap, i, j, x, y, z;
// Fill table using above recursive formula. Note that the table
// is filled in diagonal fashion (similar to http://goo.gl/PQqoS),
// from diagonal elements to table[0][n-1] which is the result.
for (gap = 0; gap < n; ++gap)
{
for (i = 0, j = gap; j < n; ++i, ++j)
{
// Here x is value of F(i+2, j), y is F(i+1, j-1) and
// z is F(i, j-2) in above recursive formula
x = ((i+2) <= j) ? table[i+2][j] : 0;
y = ((i+1) <= (j-1)) ? table[i+1][j-1] : 0;
z = (i <= (j-2))? table[i][j-2]: 0;
table[i][j] = max(arr[i] + min(x, y), arr[j] + min(y, z));
}
}
return table[0][n-1];
return ans;
}
int main(){
int t,n,i;
cin>>t;
while(t--){
memset(dp,0,sizeof dp);
cout<<"Enter the array size : ";
cin>>n;
cout<<"Enter the numbers ";
for(i=0;i<n;i++){
cin>>arr[i];
}
int ans = playOptimally(n);
}
return 0;
}