@@ -89,4 +89,169 @@ int main() {
89
89
```
90
90
91
91
</CPPSection>
92
+ <JavaSection>
93
+
94
+ ```java
95
+ import java.util.*;
96
+
97
+ public class ThreeLogos {
98
+ private static final int N = 3 ;
99
+
100
+ public static void main (String [] args ) {
101
+ Scanner scanner = new Scanner (System .in );
102
+ int[][] logos = new int[N ][2 ];
103
+ for (int i = 0; i < N; i++) {
104
+ logos[i ][0 ] = scanner.nextInt();
105
+ logos[i ][1 ] = scanner.nextInt();
106
+ }
107
+
108
+ long area = 0;
109
+ for (int[] p : logos ) { area += p[0 ] * p[1 ]; }
110
+
111
+ // if area is not a perfect square, then it's already invalid
112
+ int len = 1;
113
+ while (len * len < area) { len++; }
114
+
115
+ if (len * len != area) {
116
+ System.out.println("-1");
117
+ return;
118
+ }
119
+
120
+ // loop through all rotations of each rectangle
121
+ for (int rotateMask = 0; rotateMask < (1 << N); rotateMask++) {
122
+ char[][] grid = new char[len ][len ];
123
+ for (int i = 0; i < len; i++) {
124
+ // 'Z' represents an empty space in the grid
125
+ Arrays.fill(grid[i ], 'Z');
126
+ }
127
+ int numPlaced = 0;
128
+
129
+ outerLoop:
130
+ for (int i = 0 ; i < len ; i ++ ) {
131
+ for (int j = 0 ; j < len ; j ++) {
132
+ if (grid [i ][j ] == ' Z' ) {
133
+ if (numPlaced == N ) {
134
+ // we have already placed all the logos
135
+ break outerLoop ;
136
+ }
137
+
138
+ int w = logos [numPlaced ][0 ];
139
+ int h = logos [numPlaced ][1 ];
140
+
141
+ if ((rotateMask & (1 << numPlaced )) != 0 ) {
142
+ // rotate 90 degrees
143
+ int temp = w ;
144
+ w = h ;
145
+ h = temp ;
146
+ }
147
+
148
+ // place configuration
149
+ for (int r = i ; r < i + h ; r ++ ) {
150
+ for (int c = j ; c < j + w ; c ++ ) {
151
+ if (r >= len || c >= len || grid [r ][c ] != ' Z' ) {
152
+ // out of bounds or already a logo here
153
+ continue outerLoop ;
154
+ }
155
+ grid [r ][c ] = (char )(numPlaced + ' A' );
156
+ }
157
+ }
158
+
159
+ numPlaced ++ ;
160
+ }
161
+ }
162
+ }
163
+
164
+ // at this point, all logos must be placed
165
+ if (numPlaced == N ) {
166
+ System.out.println(len);
167
+ for (int i = 0 ; i < len ; i ++) {
168
+ System .out .println (new String (grid [i ]));
169
+ }
170
+ return;
171
+ }
172
+ }
173
+ System .out .println (" -1" );
174
+ }
175
+ }
176
+ ```
177
+
178
+ </JavaSection>
179
+ <PySection>
180
+
181
+ ```py
182
+ def main():
183
+ import sys
184
+
185
+ data = sys.stdin.read().split()
186
+
187
+ N = 3
188
+ logos = [(int(data[i * 2]), int(data[i * 2 + 1])) for i in range(N)]
189
+
190
+ area = sum(p[0] * p[1] for p in logos)
191
+
192
+ # if area is not a perfect square, then it's already invalid
193
+ len_side = 1
194
+ while len_side * len_side < area:
195
+ len_side += 1
196
+
197
+ if len_side * len_side != area:
198
+ print("-1")
199
+ return
200
+
201
+ # loop through all rotations of each rectangle
202
+ for rotate_mask in range(1 << N):
203
+ grid = [
204
+ ["Z" for _ in range(len_side)] for _ in range(len_side)
205
+ ] # temporary grid
206
+ # ' Z' represents an empty space in the grid
207
+ num_placed = 0
208
+
209
+ for i in range(len_side):
210
+ for j in range(len_side):
211
+ if grid[i][j] == " Z" :
212
+ if num_placed == N:
213
+ # we have already placed all the logos
214
+ goto_outer = True
215
+ break
216
+
217
+ w, h = logos[num_placed]
218
+
219
+ if rotate_mask & (1 << num_placed):
220
+ # rotate 90 degrees
221
+ w, h = h, w
222
+
223
+ # place configuration
224
+ if all(
225
+ 0 <= r < len_side and 0 <= c < len_side and grid[r][c] == " Z"
226
+ for r in range(i, i + h)
227
+ for c in range(j, j + w)
228
+ ):
229
+ for r in range(i, i + h):
230
+ for c in range(j, j + w):
231
+ grid[r][c] = chr(num_placed + ord("A"))
232
+
233
+ num_placed += 1
234
+ else:
235
+ goto_outer = True
236
+ break
237
+ else:
238
+ continue
239
+ break
240
+ else:
241
+ # at this point, all logos must be placed
242
+ assert num_placed == N
243
+
244
+ print(len_side)
245
+ for row in grid:
246
+ print("".join(row))
247
+ return
248
+
249
+ print("-1")
250
+
251
+
252
+ if __name__ == " __main__" :
253
+ main()
254
+ ```
255
+
256
+ </PySection>
92
257
</LanguageSection>
0 commit comments