Good evening. Apologies if the question is poorly formated as it's my first time posting here.
I'm looking for aid with a particular exercise, as I've brainstormed for almost two hours now and can't find any suitable solution. The exercise is as follows: given a certain number of wagons, two thieves will be competing for the highest profit.
The first thief, let's say A, gets to start picking first. The thieves may pick either the first or the last wagon currently available on the list. The picked wagon is then removed from the list and its value is added to a counter for that respective thief. The objective of the exercise is to obtain the highest possible profit for thief A while also ensuring thief B is attempting to do the same.
If for example, we have the following input:
6
10
150
3
7
9
9
It means there are 6 wagons, with values 10, 150, 3, 7, 9 and 9 respectfully. If following an optimal strategy, the output should be 166, assuming both thieves follow the optimal strategy. However, so far, all I've managed to obtain is 169, which is theoretically the highest result thief A can obtain regardless of how thief B plays.
I'm not seeing how I can ensure both thieves follow the optimal strategy code-wise. It's supposedly an exercise where you must check for all possible combinations, but how can I look at the results and figure out in which both thieves followed an optimal strategy? Any ideas?
The code:
#include <stdio.h>
#include <stdlib.h>
#define DEBUG 0
int max = 0;
int diff = 9999;
int heist(int *wagons, int i, int j, int carry, int turn, int pos){
#if DEBUG
printf("DEBUG! i: %d || j: %d || carry: %d || turn: %d || post: %d || max: %d\n",i,j,carry,turn,pos,max);
#endif
/* Stopping condition */
if (i==j){
if (turn) carry += wagons[i];
if (carry>=max){
max = carry;
}
return 0;
}
if (!pos){
/* First wagon */
if (turn) carry += wagons[i];
i++;
} else {
/* Last wagon */
if (turn) carry += wagons[j];
j--;
}
turn = !turn;
heist(wagons,i,j,carry,turn,0);
heist(wagons,i,j,carry,turn,1);
return 0;
}
int main()
{
/* Variables */
int n;
scanf("%d",&n);
if (!n){
printf("0\n");
return 0;
}
int wagons[n];
int i;
/* Inputs */
for (i=0;i<n;i++){
scanf("%d",&wagons[i]);
}
heist(wagons,0,n-1,0,1,0);
heist(wagons,0,n-1,0,1,1);
printf("%d\n",max);
return 0;
}