1
+ #include < bits/stdc++.h>
2
+ #define all (x ) x.begin(),x.end()
3
+ #define msg (str,str2 ) cout << str << str2<< endl
4
+ using namespace std ;
5
+
6
+ using ll = long long ;
7
+ using ld = long double ;
8
+ using uint = unsigned int ;
9
+ using ull = unsigned long long ;
10
+ template <typename T>
11
+ using pair2 = pair<T, T>;
12
+ using pii = pair<int , int >;
13
+ using pli = pair<ll, int >;
14
+ using pll = pair<ll, ll>;
15
+
16
+ #define pb push_back
17
+ #define mp make_pair
18
+
19
+ int gcd (int a,int b){
20
+ if (a%b==0 ) return b;
21
+ else return gcd (b,a%b);
22
+ }
23
+
24
+ clock_t startTime;
25
+ double getCurrentTime () {
26
+ return (double )(clock () - startTime) / CLOCKS_PER_SEC;
27
+ }
28
+ const int N = 100 ;
29
+ const int M = 100 ;
30
+ int grid[N][M];
31
+ int dp[N][M];
32
+
33
+ void solve (){
34
+ int h,w; cin>>h>>w;
35
+ for (int e = 0 ;e <h ; e++)
36
+ for (int j = 0 ; j < w;j++)
37
+ cin>>grid[e][j];
38
+ // fill the last row of dp
39
+ for (int e = 0 ;e < w ; e++) dp[h-1 ][e] = grid[h-1 ][e];
40
+ // fill de abajo hacia arriba
41
+ for (int e = h-2 ; e>=0 ; e--){
42
+ dp[e][0 ] = grid[e][0 ] + max (dp[e+1 ][0 ],dp[e+1 ][1 ]);
43
+ int j = 1 ;
44
+ for (; j < w-1 ; j++){
45
+ dp[e][j] = grid[e][j] + max (dp[e+1 ][j-1 ], max (dp[e+1 ][j], dp[e+1 ][j+1 ]));
46
+ }
47
+ dp[e][j] = grid[e][j] + max (dp[e+1 ][j-1 ], dp[e+1 ][j]);
48
+ }
49
+ // now, find the max of dp[0][e]
50
+ int ans = INT_MIN;
51
+ for (int e = 0 ;e < w ; e++){
52
+ ans = max (ans,dp[0 ][e]);
53
+ }
54
+ cout << ans << endl;
55
+ }
56
+ int main (){
57
+ ios_base::sync_with_stdio (false );
58
+ cin.tie (0 );
59
+ #ifdef DEBUG
60
+ freopen (" input.txt" ," r" ,stdin);
61
+ freopen (" output.txt" ," w" ,stdout);
62
+ #endif
63
+ int t; cin>>t;
64
+ while (t--){
65
+ solve ();
66
+ }
67
+
68
+ return 0 ;
69
+ }
0 commit comments