-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.cpp
More file actions
51 lines (41 loc) · 1.3 KB
/
11.cpp
File metadata and controls
51 lines (41 loc) · 1.3 KB
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
/* Project Euler Problem 11: https://projecteuler.net/problem=11 */
// Reads the 20x20 grid and scans every direction for the largest product of four adjacent values.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int sz = 20;
vector<vector<long long>> matrix(sz, vector<long long>(sz, 0));
for (int i = 0; i < sz; i++) {
for (int j = 0; j < sz; j++) {
cin>>matrix[i][j];
}
}
long long ans = 0;
for (int i = 0; i < sz; i++) {
for (int j = 0; j < sz; j++) {
long long product = 1;
for (int k = 0; k < 4 && j+k < sz; k++) {
product *= matrix[i][j+k];
}
ans = max(ans, product);
product = 1;
for (int k = 0; k < 4 && i+k < sz; k++) {
product *= matrix[i+k][j];
}
ans = max(ans, product);
product = 1;
for (int k = 0; k < 4 && i+k < sz && j+k < sz; k++) {
product *= matrix[i+k][j+k];
}
ans = max(ans, product);
product = 1;
for (int k = 0; k < 4 && i+k < sz && j-k >= 0; k++) {
product *= matrix[i+k][j-k];
}
ans = max(ans, product);
}
}
cout<<ans<<endl;
return 0;
}