-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdp_2_gridTraveller.cpp
More file actions
49 lines (39 loc) · 834 Bytes
/
Copy pathdp_2_gridTraveller.cpp
File metadata and controls
49 lines (39 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
using namespace std;
#define ll long long int
#define MAX 102
ll dp[MAX][MAX];
//Using Naive/ Brute-Force approach
ll solve(int m,int n){
if(m == 1 && n == 1)
return 1;
if(m == 0 || n == 0)
return 0;
return solve(m-1,n) + solve(m,n-1);
}
//Using the concept of memoisation
ll solve_dp(int m,int n){
if(!(dp[m][n])){
if(m == 1 && n == 1)
dp[m][n] = 1;
else if(m == 0 || n == 0)
dp[m][n] = 0;
else
dp[m][n] = solve(m-1,n) + solve(m,n-1);
}
return dp[m][n];
}
int main(){
//This will work with Niave approach
//cout<<solve(9,1);
//This will fail with Naive approach
//cout<<solve(9,100);
//But will work fine with the DP approach
cout<<solve_dp(9,50);
}
//Code contributed by:Abhishek Dutt
//Queries, mail at duttabhishek0@gmail.com
/*
(1,1) --> 1
(0,x) || (x,0) --> 1
*/