forked from Ashish-kumar7/geeks-for-geeks-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPossible paths.cpp
More file actions
27 lines (25 loc) · 744 Bytes
/
Copy pathPossible paths.cpp
File metadata and controls
27 lines (25 loc) · 744 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
class Solution {
public:
int dfs(vector<vector<int>>&graph, vector<vector<long>>&dp, int cur, int target, int k) {
if(dp[cur][k] != -1) {
return dp[cur][k];
}
dp[cur][k] = 0;
for(int i=0; i<graph.size(); i++) {
if(graph[cur][i]) {
dp[cur][k] = (dp[cur][k] + dfs(graph, dp, i, target, k-1))%int(1e9+7);
}
}
return dp[cur][k];
}
int MinimumWalk(vector<vector<int>>&graph, int u, int v, int k){
// Code here
vector<vector<long>> dp(graph.size(), vector<long>(k+1, -1));
dp[v][0] = 1;
for(int i=0; i<graph.size(); i++) {
if(i != v) dp[i][0] = 0;
}
dfs(graph, dp, u, v, k);
return dp[u][k];
}
}