-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXSum.cpp
72 lines (68 loc) · 1.69 KB
/
XSum.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
65
66
67
68
69
70
71
72
/*
Problem statement:
Timur's grandfather gifted him a chessboard to practice his chess skills.
This chessboard is a grid a with n rows and m columns with each cell having a non-negative integer written on it.
Timur's challenge is to place a bishop on the board such that the sum of all cells attacked by the bishop is maximal.
The bishop attacks in all directions diagonally, and there is no limit to the distance which the bishop can attack.
Note that the cell on which the bishop is placed is also considered attacked. Help him find the maximal sum he can get.
*/
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int n, m;
cin >> n >> m;
int** arr = new int*[n];
for (int i = 0; i < n; i++)
arr[i] = new int[m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
vector<int> v;
int sum1=0, sum2=0, sum3=0, sum4=0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
int ii = i - 1, jj = j - 1;
while (ii>=0 && jj>=0) {
sum1 += arr[ii][jj];
ii--;
jj--;
}
ii = i - 1, jj = j + 1;
while (ii >= 0 && jj < m) {
sum2 += arr[ii][jj];
ii--;
jj++;
}
ii = i + 1, jj = j - 1;
while (ii < n && jj >= 0) {
sum3 += arr[ii][jj];
ii++;
jj--;
}
ii = i + 1, jj = j + 1;
while (ii < n && jj < m) {
sum4 += arr[ii][jj];
ii++;
jj++;
}
v.push_back(sum1 + sum2 + sum3 + sum4 + arr[i][j]);
}
}
int z = INT_MIN;
for (int i = 0; i < v.size(); i++) {
if (v[i] > z) {
z = v[i];
}
}
cout << z << endl;
}
return 0;
}