Search code examples
c++recursiondynamicc++14memoization

Wrong Solution if Memoization is added to Recursion


I have created a DP program but the problem is that I get correct answers when I don't use memoization. As soon as I introduce memoization, I start getting the wrong answers for some problems

Here is the code in C++ 14 with memoization turned off (By commenting)

#include <iostream>
#include <math.h>
#include<algorithm>

using namespace std;
int max_Number_of_turns;
int dp[9999][1000];
int changeTheDigit(int n, int d) {
    int rem = n % (int) (pow(10, 4 - d));
    n /= (pow(10, 4 - d));
    int x = n % 10;
    n /= 10;
    if (x == 9) x = 0;
    else x = x + 1;
    n = n * (10) + x;
    n = n * (pow(10, 4 - d)) + rem;
    return n;
}

int minMax(int n, int t) {
    int ans =0;
    //if(dp[n][t]>=0) { return dp[n][t];}
    if (t > max_Number_of_turns) return n;

    int N;
    for (int i = 0; i < 4; i++) {
        N = changeTheDigit(n, i + 1);
        if (t % 2 == 0) {
            //Manish chance
            if(ans==0) ans=minMax(N, t+1);
            else ans = min(ans, minMax(N, t + 1));
        } else {
            //Nitish Chance
            ans = max(ans, minMax(N, t + 1));
        }
    }
    //cout << ans << endl;
    dp[n][t]=ans;
    return ans;
}

using namespace std;

int main() {
    int T, N, M;
    cin >> T;
    while (T--) {
        cin >> N >> M;
        max_Number_of_turns=M;
        for(int i=0;i<9999;i++)
            for(int j=0;j<1000;j++)
                dp[i][j]=-1;
        if(minMax(N,1)>N){
            cout << "Nitish" << endl;
        }
        else{
            cout << "Manish" << endl;
        }
    }
    return 0;
}

Turn the memoization comment on (i.e. remove the comments from this line)

if(dp[n][t]>=0) { return dp[n][t];}

and my code will give wrong answers to some problems For example, let us consider the input

1
4569 12

Original Correct Solution is Manish But If I turn on memoization, My solution is Nitish

Can you suggest me that what am I doing wrong here

Also, a fun fact is that, if the change the DP code from

if(dp[n][t]>=0) { return dp[n][t];}

to

if(dp[n][t]>0) { return dp[n][t];}

Then everything is fine


Solution

  • Your problem is that the values for n and/or t are not checked and so could cause out-of-bounds issues with the array. You can see that if you insert the following at the start of your minMax function:

    if (n < 0 || n >= 9999) cout << "n invalid at " << n << '\n';
    if (t < 0 || t >= 1000) cout << "t invalid at " << t << '\n';
    

    Running that with your sample input gives warnings before outputting the result:

    n invalid at 9999
    n invalid at 9999
    n invalid at 9999
    

    To fix this, you can just ensure you only use memoisation when you have enough storage for it, first when checking the value:

    if (n >= 0 && n < 9999 && t >= 0 && t < 1000 && dp[n][t] >= 0)
        return dp[n][t];
    

    and, second, when storing the value:

    if (n >= 0 && n < 9999 && t >= 0 && t < 1000)
        dp[n][t] = ans;