-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath in Matrix.cpp
More file actions
35 lines (30 loc) · 814 Bytes
/
Path in Matrix.cpp
File metadata and controls
35 lines (30 loc) · 814 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
#include<bits/stdc++.h>
using namespace std;
int maxPath(int);
int main(){
int t, n;
cin >> t;
while(t--){
cin >> n;
cout << maxPath(n) << endl;
}
return 0;
}
int maxPath(int n){
int mat[n][n], max;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> mat[i][j];
for(int i = 1; i < n; i++)
for(int j = 0; j < n; j++){
max = INT_MIN;
if(max < mat[i - 1][j])
max = mat[i - 1][j];
if(j + 1 < n && max < mat[i - 1][j + 1])
max = mat[i - 1][j + 1];
if(0 <= j - 1 && max < mat[i - 1][j - 1])
max = mat[i - 1][j - 1];
mat[i][j] = mat[i][j] + max;
}
return *max_element(&mat[n - 1][0], &mat[n - 1][n - 1] + 1);
}