-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMaximum_subMatrix.cpp
More file actions
50 lines (50 loc) · 897 Bytes
/
Maximum_subMatrix.cpp
File metadata and controls
50 lines (50 loc) · 897 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
50
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int Arr[N][N];
int temp[N];
int kadane(int arr[], int n) {
int sum = 0;
int ans = INT_MIN;
for (int i = 0; i < n; i++) {
if (sum + arr[i] > 0)
sum = sum + arr[i];
else
sum = 0;
ans = max(ans, sum);
}
return ans;
}
void Add(int A[], int B[], int n) {
for (int i = 0; i < n; i++) {
A[i] += B[i];
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &Arr[i][j]);
}
}
int Ans = 0;
for (int i = 0; i < n; i++) {
memset(temp, 0, sizeof(temp));
for (int j = i; j < n; j++) {
Add(temp, Arr[j], m);
Ans = max(Ans, kadane(temp, m));
}
}
printf("%d\n", Ans);
}
return 0;
}