Getting tle on my solution even when it is matching with accepted solutions.

Hey guys,
Can anybody explain why am i getting tle on the below mentioned problem.
I checked it with the accepted solutions but can’t find any difference.
Problem: https://www.spoj.com/problems/ABCPATH/
My solution: https://ideone.com/BCAd0l

I think you don’t have a clear notion about what memorization is. You are returning when dp[i][j] when its equal to -2 which is never attained. So basically your code remains just a recursion with no memorization. Have a look at how to implement the memorization process: here.

Thanks, there was a missing if condition which was necessary for memorization to work.
Also the -2 concept was useless.