-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.cpp
More file actions
103 lines (88 loc) · 1.68 KB
/
Copy path11.cpp
File metadata and controls
103 lines (88 loc) · 1.68 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
In the 20×20 grid input, four numbers along a diagonal line have been marked in red.
*/
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
#define L 4
// globals
int R, C;
int **matrix;
int maxSum = 0;
int dirY[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dirX[] = {-1, 0, 1, -1, 1, -1, 0, 1};
bool checkBounds(int x, int y) {
if (x < 0 || x >= C) {
return false;
}
if (y < 0 || y >= R) {
return false;
}
return true;
}
void start() {
int x, y;
int sum;
int mx, my;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
for (int d = 0; d < 8; d++) { // 8 directions
y = i;
x = j;
sum = 1;
for (int k = 0; k < L; k++) {
if (checkBounds(x, y)) {
sum *= matrix[y][x];
y += dirY[d];
x += dirX[d];
} else {
sum = 1;
break;
}
}
if (sum > maxSum) {
mx = x;
my = y;
maxSum = sum;
}
}
}
}
cout << mx << " " << my << " " << endl;
cout << maxSum << endl;
}
void allocateMatrix() {
matrix = new int *[R];
for (int i = 0; i < R; ++i) {
matrix[i] = new int[C];
}
}
void freeMatrix() {
for (int i = 0; i < R; ++i) {
delete[] matrix[i];
}
delete[] matrix;
}
void printMatrix() {
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cout << setw(4) << matrix[i][j];
}
cout << endl;
}
}
int main(void) {
cin >> R >> C;
allocateMatrix();
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cin >> matrix[i][j];
}
}
printMatrix();
start();
freeMatrix();
return 0;
}