-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubRectangle max sum (108).cpp
90 lines (54 loc) · 1.47 KB
/
SubRectangle max sum (108).cpp
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
64
#include <iostream>
#include <typeinfo>
#include <climits>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <forward_list>
#include <set>
#include <map>
#include <utility>
#include <iomanip>
#include <cstring>
using namespace std;
int main() {
int n;
while (cin >> n) {
int tmp, sum = 0;
int arr[101][101] = {};
int sumArr[101][101] = {};
for (int i = 1; i <= n ; i++) {
sum = 0;
for (int j = 1; j <= n ; j++) {
cin >> tmp;
sum += tmp;
arr[i][j] == tmp;
sumArr[i][j] = sum + sumArr[i-1][j];
}
}
int maxSum = 0;
int currSum = 0;
int bottomRight;
int bottomLeft;
int topRight;
int topLeft;
for (int i = 1; i <= n ; i++) {
for (int j = 1; j <= n ; j++) {
for (int g = i; g <= n; g++){
for(int k = j; k <= n; k++){
bottomRight = sumArr[g][k];
bottomLeft = sumArr[g][j-1];
topRight = sumArr[i-1][k];
topLeft = sumArr[i-1][j-1];
currSum = bottomRight - bottomLeft - topRight + topLeft;
maxSum = max(maxSum, currSum);
}
}
}
}
cout << maxSum << endl;
}
return 0;
}