-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gold Mine Problem
63 lines (49 loc) · 1.33 KB
/
Gold Mine Problem
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution{
public:
int solve(int i,int j,vector<vector<int>>&mat,vector<vector<int>>&dp){
int n=mat.size();
int m=mat[0].size();
if(j>=m)return 0;
if(dp[i][j]!=-1)return dp[i][j];
int frwd=mat[i][j]+solve(i,j+1,mat,dp);
int topfrwd=mat[i][j];
if(i-1>=0)topfrwd+=solve(i-1,j+1,mat,dp);
int botfrwd=mat[i][j];
if(i+1<n)botfrwd+=solve(i+1,j+1,mat,dp);
return dp[i][j]=max(frwd,max(topfrwd,botfrwd));
}
int maxGold(int n, int m, vector<vector<int>> mat)
{
// code here
int maxi=-1e9;
vector<vector<int>>dp(n,vector<int>(m,-1));
for(int i=0;i<n;i++){
maxi=max(maxi,solve(i,0,mat,dp));
}
return maxi;
}
};
//{ Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--){
int n, m;
cin>>n>>m;
vector<vector<int>> M(n, vector<int>(m, 0));
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++)
cin>>M[i][j];
}
Solution ob;
cout<<ob.maxGold(n, m, M)<<"\n";
}
return 0;
}
// } Driver Code Ends