1
+ #include < iostream>
2
+ using namespace std ;
3
+ /*
4
+ Check all paper
5
+ Use recursion
6
+ Get the largest sum
7
+ if earned sum is larger than global biggest then replace the biggest
8
+ printout biggest
9
+ return
10
+
11
+ Type: Bar, Cube, L, S, T
12
+ Direction: Right, Down, Left, Up
13
+ RRR DDD LLL UUU
14
+ RDL DLU LUR URD
15
+ RRU DDR LLD UUL
16
+ RUR DRD LDL ULU
17
+ R(RD) D(DL) L(LU) U(UR)
18
+ */
19
+ enum TetrominoType {BAR=0 , CUBE, L, S, T};
20
+ enum TetrominoDire {RIGHT=0 , DOWN, LEFT, UP};
21
+ const int PAPER_DIMENSION = 2 ;
22
+
23
+ int checkAndReturn (int ** paper, int N, int M, int row, int col) {
24
+ if ((row < 0 ) || (row >= N) || (col < 0 ) || (col >= M))
25
+ return -1 ;
26
+ return paper[row][col];
27
+ }
28
+
29
+ int findSum (int ** paper, int N, int M, int row, int col, TetrominoType type, TetrominoDire dire) {
30
+ int sum = paper[row][col];
31
+ int add1, add2, add3;
32
+ int add4 = -1 ; int add5 = -1 ; int add6 = -1 ;
33
+
34
+ if (type == BAR) {
35
+ if (dire == RIGHT) {
36
+ add1 = checkAndReturn (paper, N, M, row, col+1 );
37
+ add2 = checkAndReturn (paper, N, M, row, col+2 );
38
+ add3 = checkAndReturn (paper, N, M, row, col+3 );
39
+ }
40
+ else if (dire == DOWN) {
41
+ add1 = checkAndReturn (paper, N, M, row+1 , col);
42
+ add2 = checkAndReturn (paper, N, M, row+2 , col);
43
+ add3 = checkAndReturn (paper, N, M, row+3 , col);
44
+ }
45
+ else if (dire == LEFT) {
46
+ add1 = checkAndReturn (paper, N, M, row, col-1 );
47
+ add2 = checkAndReturn (paper, N, M, row, col-2 );
48
+ add3 = checkAndReturn (paper, N, M, row, col-3 );
49
+ }
50
+ else if (dire == UP) {
51
+ add1 = checkAndReturn (paper, N, M, row-1 , col);
52
+ add2 = checkAndReturn (paper, N, M, row-2 , col);
53
+ add3 = checkAndReturn (paper, N, M, row-3 , col);
54
+ }
55
+ }
56
+ else if (type == CUBE) { // RDL DLU LUR URD
57
+ if (dire == RIGHT) {
58
+ add1 = checkAndReturn (paper, N, M, row, col+1 );
59
+ add2 = checkAndReturn (paper, N, M, row+1 , col+1 );
60
+ add3 = checkAndReturn (paper, N, M, row+1 , col);
61
+ }
62
+ else if (dire == DOWN) {
63
+ add1 = checkAndReturn (paper, N, M, row+1 , col);
64
+ add2 = checkAndReturn (paper, N, M, row+1 , col-1 );
65
+ add3 = checkAndReturn (paper, N, M, row, col-1 );
66
+ }
67
+ else if (dire == LEFT) {
68
+ add1 = checkAndReturn (paper, N, M, row, col-1 );
69
+ add2 = checkAndReturn (paper, N, M, row-1 , col-1 );
70
+ add3 = checkAndReturn (paper, N, M, row-1 , col);
71
+ }
72
+ else if (dire == UP) {
73
+ add1 = checkAndReturn (paper, N, M, row-1 , col);
74
+ add2 = checkAndReturn (paper, N, M, row-1 , col+1 );
75
+ add3 = checkAndReturn (paper, N, M, row, col+1 );
76
+ }
77
+ }
78
+ else if (type == L) { // RRU DDR LLD UUL
79
+ if (dire == RIGHT) {
80
+ add1 = checkAndReturn (paper, N, M, row, col+1 );
81
+ add2 = checkAndReturn (paper, N, M, row, col+2 );
82
+ add3 = checkAndReturn (paper, N, M, row-1 , col+2 );
83
+ add4 = add1;
84
+ add5 = add2;
85
+ add6 = checkAndReturn (paper, N, M, row+1 , col+2 );
86
+ }
87
+ else if (dire == DOWN) {
88
+ add1 = checkAndReturn (paper, N, M, row+1 , col);
89
+ add2 = checkAndReturn (paper, N, M, row+2 , col);
90
+ add3 = checkAndReturn (paper, N, M, row+2 , col+1 );
91
+ add4 = add1;
92
+ add5 = add2;
93
+ add6 = checkAndReturn (paper, N, M, row+2 , col-1 );
94
+ }
95
+ else if (dire == LEFT) {
96
+ add1 = checkAndReturn (paper, N, M, row, col-1 );
97
+ add2 = checkAndReturn (paper, N, M, row, col-2 );
98
+ add3 = checkAndReturn (paper, N, M, row+1 , col-2 );
99
+ add4 = add1;
100
+ add5 = add2;
101
+ add6 = checkAndReturn (paper, N, M, row-1 , col-2 );
102
+ }
103
+ else if (dire == UP) {
104
+ add1 = checkAndReturn (paper, N, M, row-1 , col);
105
+ add2 = checkAndReturn (paper, N, M, row-2 , col);
106
+ add3 = checkAndReturn (paper, N, M, row-2 , col-1 );
107
+ add4 = add1;
108
+ add5 = add2;
109
+ add6 = checkAndReturn (paper, N, M, row-2 , col+1 );
110
+ }
111
+ }
112
+ else if (type == S) { // RUR DRD LDL ULU
113
+ if (dire == RIGHT) {
114
+ add1 = checkAndReturn (paper, N, M, row, col+1 );
115
+ add2 = checkAndReturn (paper, N, M, row-1 , col+1 );
116
+ add3 = checkAndReturn (paper, N, M, row-1 , col+2 );
117
+ add4 = add1;
118
+ add5 = checkAndReturn (paper, N, M, row+1 , col+1 );
119
+ add6 = checkAndReturn (paper, N, M, row+1 , col+2 );
120
+ }
121
+ else if (dire == DOWN) {
122
+ add1 = checkAndReturn (paper, N, M, row+1 , col);
123
+ add2 = checkAndReturn (paper, N, M, row+1 , col+1 );
124
+ add3 = checkAndReturn (paper, N, M, row+2 , col+1 );
125
+ add4 = add1;
126
+ add5 = checkAndReturn (paper, N, M, row+1 , col-1 );
127
+ add6 = checkAndReturn (paper, N, M, row+2 , col-1 );
128
+ }
129
+ else if (dire == LEFT) {
130
+ add1 = checkAndReturn (paper, N, M, row, col-1 );
131
+ add2 = checkAndReturn (paper, N, M, row+1 , col-1 );
132
+ add3 = checkAndReturn (paper, N, M, row+1 , col-2 );
133
+ add4 = add1;
134
+ add5 = checkAndReturn (paper, N, M, row-1 , col-1 );
135
+ add6 = checkAndReturn (paper, N, M, row-1 , col-2 );
136
+ }
137
+ else if (dire == UP) {
138
+ add1 = checkAndReturn (paper, N, M, row-1 , col);
139
+ add2 = checkAndReturn (paper, N, M, row-1 , col-1 );
140
+ add3 = checkAndReturn (paper, N, M, row-2 , col-1 );
141
+ add4 = add1;
142
+ add5 = checkAndReturn (paper, N, M, row-1 , col+1 );
143
+ add6 = checkAndReturn (paper, N, M, row-2 , col+1 );
144
+ }
145
+ }
146
+ else if (type == T) { // R(RD) D(DL) L(LU) U(UR)
147
+ if (dire == RIGHT) {
148
+ add1 = checkAndReturn (paper, N, M, row, col+1 );
149
+ add2 = checkAndReturn (paper, N, M, row, col+2 );
150
+ add3 = checkAndReturn (paper, N, M, row+1 , col+1 );
151
+ add4 = add1;
152
+ add5 = add2;
153
+ add6 = checkAndReturn (paper, N, M, row-1 , col+1 );
154
+ }
155
+ else if (dire == DOWN) {
156
+ add1 = checkAndReturn (paper, N, M, row+1 , col);
157
+ add2 = checkAndReturn (paper, N, M, row+2 , col);
158
+ add3 = checkAndReturn (paper, N, M, row+1 , col-1 );
159
+ add4 = add1;
160
+ add5 = add2;
161
+ add3 = checkAndReturn (paper, N, M, row+1 , col+1 );
162
+ }
163
+ else if (dire == LEFT) {
164
+ add1 = checkAndReturn (paper, N, M, row, col-1 );
165
+ add2 = checkAndReturn (paper, N, M, row, col-2 );
166
+ add3 = checkAndReturn (paper, N, M, row-1 , col-1 );
167
+ add4 = add1;
168
+ add5 = add2;
169
+ add3 = checkAndReturn (paper, N, M, row+1 , col-1 );
170
+ }
171
+ else if (dire == UP) {
172
+ add1 = checkAndReturn (paper, N, M, row-1 , col);
173
+ add2 = checkAndReturn (paper, N, M, row-2 , col);
174
+ add3 = checkAndReturn (paper, N, M, row-1 , col+1 );
175
+ add4 = add1;
176
+ add5 = add2;
177
+ add3 = checkAndReturn (paper, N, M, row-1 , col-1 );
178
+ }
179
+ }
180
+ if (((add1 < 0 ) || (add2 < 0 ) || (add3 < 0 )) && ((add4 < 0 ) || (add5 < 0 ) || (add6 < 0 )))
181
+ return 0 ;
182
+ int candidateA = add1 + add2 + add3;
183
+ int candidateB = add4 + add5 + add6;
184
+ sum += (candidateA < candidateB) ? candidateB : candidateA;
185
+ return sum;
186
+ }
187
+
188
+ int findBiggestSum (int ** paper, int N, int M, int row, int col) {
189
+ int biggest = 0 ;
190
+
191
+ for (int type = 0 ; type < 5 ; ++type)
192
+ for (int dire = 0 ; dire < 4 ; ++dire) {
193
+ int temp = findSum (paper, N, M, row, col, static_cast <TetrominoType>(type), static_cast <TetrominoDire>(dire));
194
+ biggest = (biggest < temp) ? temp : biggest;
195
+ }
196
+
197
+ return biggest;
198
+ }
199
+
200
+ int main () {
201
+ ios_base::sync_with_stdio (false );
202
+ cin.tie (nullptr );
203
+
204
+ int N, M, biggest = 0 ;
205
+ cin >> N >> M;
206
+ int ** paper = new int *[N];
207
+ for (int i = 0 ; i < N; ++i) {
208
+ paper[i] = new int [M];
209
+ for (int j = 0 ; j < M; ++j)
210
+ cin >> paper[i][j];
211
+ }
212
+
213
+ for (int row = 0 ; row < N; ++row)
214
+ for (int col = 0 ; col < M; ++col) {
215
+ int temp = findBiggestSum (paper, N, M, row, col);
216
+ biggest = (biggest < temp) ? temp : biggest;
217
+ }
218
+
219
+ cout << biggest << ' \n ' ;
220
+
221
+ for (int i = 0 ; i < N; ++i) {
222
+ delete [] paper[i];
223
+ paper[i] = nullptr ;
224
+ }
225
+ delete [] paper;
226
+
227
+ return 0 ;
228
+ }
0 commit comments