|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.StringTokenizer; |
| 4 | + |
| 5 | +public class BJ_18808_스티커_붙이기 { |
| 6 | + |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static StringTokenizer st; |
| 10 | + |
| 11 | + private static int N, M, K, R, C, ans; |
| 12 | + private static int[][] laptop, sticker, rotated; |
| 13 | + |
| 14 | + public static void main(String[] args) throws IOException { |
| 15 | + init(); |
| 16 | + sol(); |
| 17 | + } |
| 18 | + |
| 19 | + private static void init() throws IOException { |
| 20 | + st = new StringTokenizer(br.readLine()); |
| 21 | + N = Integer.parseInt(st.nextToken()); |
| 22 | + M = Integer.parseInt(st.nextToken()); |
| 23 | + K = Integer.parseInt(st.nextToken()); |
| 24 | + |
| 25 | + laptop = new int[N][M]; |
| 26 | + } |
| 27 | + |
| 28 | + private static void sol() throws IOException { |
| 29 | + while (K-- > 0) { |
| 30 | + st = new StringTokenizer(br.readLine()); |
| 31 | + R = Integer.parseInt(st.nextToken()); |
| 32 | + C = Integer.parseInt(st.nextToken()); |
| 33 | + |
| 34 | + sticker = new int[R][C]; |
| 35 | + for (int i = 0; i < R; i++) { |
| 36 | + st = new StringTokenizer(br.readLine()); |
| 37 | + for (int j = 0; j < C; j++) { |
| 38 | + sticker[i][j] = Integer.parseInt(st.nextToken()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + for (int dir = 0; dir < 4; dir++) { |
| 43 | + if (attach()) { |
| 44 | + break; |
| 45 | + } |
| 46 | + rotate(dir); |
| 47 | + } |
| 48 | + } |
| 49 | + printAns(); |
| 50 | + } |
| 51 | + |
| 52 | + private static boolean attach() { |
| 53 | + for (int i = 0; i < N; i++) { |
| 54 | + for (int j = 0; j < M; j++) { |
| 55 | + // 스티커가 노트북 범위를 벗어남 |
| 56 | + if (i + R - 1 >= N || j + C - 1 >= M) continue; |
| 57 | + |
| 58 | + // sticker attached |
| 59 | + if (checkValid(i, j)) return true; |
| 60 | + } |
| 61 | + } |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + private static boolean checkValid(int x, int y) { |
| 66 | + for (int i = 0; i < R; i++) { |
| 67 | + for (int j = 0; j < C; j++) { |
| 68 | + // attachment unavailable |
| 69 | + if (laptop[x + i][y + j] == 1 && sticker[i][j] == 1) return false; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + for (int i = 0; i < R; i++) { |
| 74 | + for (int j = 0; j < C; j++) { |
| 75 | + if (sticker[i][j] == 0) continue; |
| 76 | + |
| 77 | + laptop[x + i][y + j] = 1; |
| 78 | + } |
| 79 | + } |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + private static void rotate(int dir) { |
| 84 | + int nR = C; |
| 85 | + int nC = R; |
| 86 | + |
| 87 | + rotated = new int[nR][nC]; |
| 88 | + for (int i = 0; i < nR; i++) { |
| 89 | + for (int j = 0; j < nC; j++) { |
| 90 | + rotated[i][j] = sticker[R - j - 1][i]; |
| 91 | + } |
| 92 | + } |
| 93 | + R = nR; |
| 94 | + C = nC; |
| 95 | + sticker = rotated; |
| 96 | + } |
| 97 | + |
| 98 | + private static void printAns() throws IOException { |
| 99 | + for (int i = 0; i < N; i++) { |
| 100 | + for (int j = 0; j < M; j++) { |
| 101 | + if (laptop[i][j] == 1) ans++; |
| 102 | + } |
| 103 | + } |
| 104 | + bw.write(ans + ""); |
| 105 | + bw.flush(); |
| 106 | + bw.close(); |
| 107 | + br.close(); |
| 108 | + } |
| 109 | + |
| 110 | +} |
| 111 | +``` |
0 commit comments