Algorithm Analysis
We will implement a recursive function with memoization.
Algorithm Implementation
Declare a two-dimensional array for memoizing the function values: dp[x][y] = f(x, y)
.
long long dp[51][51];
Implementation of the recursive function with memoization.
long long f(int x, int y) { if (x <= 0 || y <= 0) return 0; if (dp[x][y] != -1) return dp[x][y]; if (x <= y) return dp[x][y] = f(x-1,y-2) + f(x-2,y-1) + 2; return dp[x][y] = f(x-2,y-2) + 1; }
The main part of the program. Read the input data. Initialize the dp
array cells with the value -1
. Call the function f(x, y)
and print its value.
scanf("%d %d",&x,&y); memset(dp,-1,sizeof(dp)); printf("%lld\n",f(x,y));