|
| 1 | +package org.example._48week; |
| 2 | + |
| 3 | +class LockAndKey { |
| 4 | + private static int N = 0; |
| 5 | + private static int M = 0; |
| 6 | + |
| 7 | + public static void main(String[] args) { |
| 8 | + int[][] key = {{0, 0, 0}, {1, 0, 0}, {0, 1, 1}}; |
| 9 | + int[][] lock = {{1, 1, 1}, {1, 1, 0}, {1, 0, 1}}; |
| 10 | + |
| 11 | + boolean solution = solution(key, lock); |
| 12 | + System.out.println(solution); |
| 13 | + } |
| 14 | + public static boolean solution(int[][] key, int[][] lock) { |
| 15 | + N = lock.length; |
| 16 | + M = key.length; |
| 17 | + |
| 18 | + int[][][] keys = makeRotationKeys(key); |
| 19 | + int[][] newLock = makeExtensionLock(lock); |
| 20 | + |
| 21 | + for (int row = 0; row < newLock.length; row++) { |
| 22 | + for (int col = 0; col < newLock.length; col++) { |
| 23 | + for (int rotation = 0; rotation < 4; rotation++) { |
| 24 | + boolean isMatch = isMatch(keys[rotation], newLock, row, col); |
| 25 | + if (isMatch) { |
| 26 | + return true; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + public static int[][][] makeRotationKeys(int[][] key) { |
| 36 | + int[][][] keys = new int[4][M][M]; |
| 37 | + keys[0] = key; |
| 38 | + |
| 39 | + for (int i = 1; i < 4; i++) { |
| 40 | + for (int row = 0; row < key.length; row++) { |
| 41 | + for (int col = 0; col < key[0].length; col++) { |
| 42 | + keys[i][col][key.length-1-row] = keys[i - 1][row][col]; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return keys; |
| 48 | + } |
| 49 | + |
| 50 | + private static int[][] makeExtensionLock(int[][] lock) { |
| 51 | + int newLength = N + 2 * M - 2; |
| 52 | + int[][] newLock = new int[newLength][newLength]; |
| 53 | + |
| 54 | + for (int row = 0; row < newLength; row++) { |
| 55 | + for (int col = 0; col < newLength; col++) { |
| 56 | + newLock[row][col] = 3; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + for (int row = M - 1, originalRow = 0; row < M - 1 + N; row++, originalRow++) { |
| 61 | + for (int col = M - 1, originalCol = 0; col < M - 1 + N; col++, originalCol++) { |
| 62 | + newLock[row][col] = lock[originalRow][originalCol]; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return newLock; |
| 67 | + } |
| 68 | + |
| 69 | + public static boolean isMatch(int[][] key, int[][] lock, int rowIndex, int colIndex) { |
| 70 | + for (int row = M - 1; row < M - 1 + N; row++) { |
| 71 | + for (int col = M - 1; col < M - 1 + N; col++) { |
| 72 | + if (((row - rowIndex) < 0 || (row - rowIndex) >= M) || ((col - colIndex) < 0 || (col - colIndex) >= M)){ |
| 73 | + if(lock[row][col]==0){ |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + if (lock[row][col] == 0 && key[row - rowIndex][col - colIndex] == 0) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + if (lock[row][col] == 1 && key[row - rowIndex][col - colIndex] == 1) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return true; |
| 91 | + } |
| 92 | +} |
0 commit comments