|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + static int N, M, K; |
| 8 | + static int[][] note; |
| 9 | + static int R, C; |
| 10 | + static int[][] sticker; |
| 11 | + |
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 15 | + |
| 16 | + N = Integer.parseInt(st.nextToken()); |
| 17 | + M = Integer.parseInt(st.nextToken()); |
| 18 | + K = Integer.parseInt(st.nextToken()); |
| 19 | + |
| 20 | + note = new int[N][M]; |
| 21 | + |
| 22 | + for (int s = 0; s < K; s++) { |
| 23 | + st = new StringTokenizer(br.readLine()); |
| 24 | + R = Integer.parseInt(st.nextToken()); |
| 25 | + C = Integer.parseInt(st.nextToken()); |
| 26 | + sticker = new int[R][C]; |
| 27 | + |
| 28 | + for (int i = 0; i < R; i++) { |
| 29 | + st = new StringTokenizer(br.readLine()); |
| 30 | + for (int j = 0; j < C; j++) sticker[i][j] = Integer.parseInt(st.nextToken()); |
| 31 | + } |
| 32 | + |
| 33 | + for (int d = 0; d < 4; d++) { |
| 34 | + if (tryAttach()) break; |
| 35 | + rotate(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + int count = 0; |
| 40 | + for (int i = 0; i < N; i++) { |
| 41 | + for (int j = 0; j < M; j++) if (note[i][j] == 1) count++; |
| 42 | + } |
| 43 | + System.out.println(count); |
| 44 | + } |
| 45 | + |
| 46 | + static boolean tryAttach() { |
| 47 | + for (int i = 0; i <= N - R; i++) { |
| 48 | + for (int j = 0; j <= M - C; j++) { |
| 49 | + if (canAttach(i, j)) { |
| 50 | + attach(i, j); |
| 51 | + return true; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + static boolean canAttach(int y, int x) { |
| 59 | + for (int i = 0; i < R; i++) { |
| 60 | + for (int j = 0; j < C; j++) { |
| 61 | + if (sticker[i][j] == 1 && note[y + i][x + j] == 1) return false; |
| 62 | + } |
| 63 | + } |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + static void attach(int y, int x) { |
| 68 | + for (int i = 0; i < R; i++) { |
| 69 | + for (int j = 0; j < C; j++) { |
| 70 | + if (sticker[i][j] == 1) note[y + i][x + j] = 1; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + static void rotate() { |
| 76 | + int[][] temp = new int[C][R]; |
| 77 | + for (int i = 0; i < R; i++) { |
| 78 | + for (int j = 0; j < C; j++) { |
| 79 | + temp[j][R - 1 - i] = sticker[i][j]; |
| 80 | + } |
| 81 | + } |
| 82 | + sticker = temp; |
| 83 | + int t = R; |
| 84 | + R = C; |
| 85 | + C = t; |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
0 commit comments