I'm dealing with a problem, trying to make a recursive method that will test all possible combinations and show me the "cheapest" one.
The task is that:
- I have an Array[n] numbers, which was dynamically allocated and filled. {1, 3, 4, 2} for example. The array is defined as
int *array;
- I fond out the size of the array and putted in the variable Size.
- Each 2 numbers in the array indicate the top and bottom of a segment, so in this case the array has 3 segments. The segments are: A = 1 3 ,B = 3 4 ,C = 4 2 .
- I have to join them in the order they are. I can start joining from the last two to the first one or I can start from the center to the sides but I have to choose the "cheapest" way.
- Every time i join two segments: A = 1 3 and B = 3 4 . I calculate the cost of it this way: (1 * 4) + 3. I multiply the numbers at the ends of the segments and Sum to the the number between the segments.
- The input will always be 2 or more segments meaning 3 or more numbers in the Array.
Practically what I need it to do is:
For two segments: array= "1 2 3"
A={1,2},B={2,3} ---- (1*3)+2 -> Cost = 5
For three segments: array= "30 20 25 10"
A={30,20},B={20,25},C={25,10} ----
case1 - [(30*25)+20] + [(30*10)+25] -> Cost = 1095
case2 - [(20*10)+25] + [(30*10)+20] /> Cost = 545
So we can clearly see that the second cost is better so we return that value. For more segments the number of calculations will drastically increase but that's ok for what I need.
I've been trying many times in different ways but I'm getting confused with all the pointers and also that I'm not good with recursion functions. I don't know if I was specific enough, if something isn't clear I'll be happy to explain.
I sure dont need the whole code, maybe some explanaition or an idea about how to do it would be appresiated :)