DFS C++ Elegant Code


#1
int X,Y,dp[5000][5000];
int dfs(int x,int y){
    if(x>X)return 0;
    if(y>Y)return 0;
    if(x==X and y==Y)return 1;
    if(dp[x][y])return dp[x][y];
    return dp[x][y]=dfs(x+1,y)+dfs(x,y+1);
}
int Solution::uniquePaths(int A, int B) {
    X=A-1,Y=B-1;
    for(int i=0;i<=A;i++)for(int j=0;j<=B;j++)dp[i][j]=0;
    return dfs(0,0);
}