0

So ive got this recursive function:

int solution(int i, int j, int n, int m, int **A, int c, int **B, int **C, int *D, int *E) {
    int a, b, k, w;

    if (i < n && j < m) {
        B[i + j][0] = i;
        B[i + j][1] = j;
        D[i + j] = c;
    }
    else if (i == n && j == m) {
        B[i + j - 1][0] = i;
        B[i + j - 1][1] = j;
        D[i + j - 1] = c;
    }

    if (A[i][j] == 'C') {
        c++;
    }

    if (i == n - 1 && j == m - 1) {
        if (c > maxcoins) {
            for (w = 0; w < n + m; w++) {
                C[w][0] = B[w][0];
                C[w][1] = B[w][1];
                E[w] = D[w];
            }

            maxcoins = c;
        }
    }

    if (i < n - 1) {
        a = i + 1;
        k = solution(a, j, n, m, A, c, B, C, D, E);
    }

    if (j < m - 1) {
        b = j + 1;
        k = solution(i, b, n, m, A, c, B, C, D, E);
    }
}

and i want to convert it to a dynamic programming solution but i cant figure out how to do it. Basically given a 2D array of C and . ,C representing coins and . blank spots it has to figure out the maximum number of coins one can collect only by moving down and right(maxcoins is an external variable). I would appreciate it if you got any answears that might help, thanks!

Chris
  • 26,361
  • 5
  • 21
  • 42
xKostmand
  • 1
  • 1
  • [This link](https://stackoverflow.com/questions/12133754/whats-the-difference-between-recursion-memoization-dynamic-programming) (as well as to the link it contains) has a lot to say on this topic. It should help to answer your questions. – ryyker Jan 17 '22 at 17:26
  • 1
    The function is declared with a return type of `int` but does not return a value. – Ian Abbott Jan 17 '22 at 17:28
  • @IanAbbott, don't worry, `k` isn't used anywhere. ;) – Wyck Jan 17 '22 at 17:42
  • You should worry, because "unnecessary" warnings will distract from more serious ones. – Weather Vane Jan 17 '22 at 18:15
  • @ryyker ive read several articles discussing about recursive and dynamic programming including the one you mentioned but i havent found and solutions to my problem. Do you by any chance have figured it out? – xKostmand Jan 17 '22 at 18:23
  • read this link [mcve], then do what it says. This should include how you called this function from eg. `main()` showing exactly what input values you pass as arguments and what corresponding results you expect. For now you've provided no information saying what you've tried, or what _specific_ errors or problems you have seen. – ryyker Jan 17 '22 at 20:03

0 Answers0