2020 KAKAO BLIND RECRUITMENT 자물쇠와 열쇠
유형
코드
class Solution {
public boolean solution(int[][] key, int[][] lock) {
boolean answer = false;
int numOfEmpty = getNumOfEmpty(lock);
int startRow = 0;
int startCol = 0;
int numOfIteration = lock.length + key.length - 1;
for (int rotate = 0; rotate < 4; rotate++) {
key = rotate(key);
for (int i = 0; i < numOfIteration; i++) {
for (int j = 0; j < numOfIteration; j++) {
startRow = -key.length + 1 + i;
startCol = -key.length + 1 + j;
int cnt = 0;
boolean stopflag = false;
for (int k = 0; k < key.length; k++) {
for (int l = 0; l < key.length; l++) {
if(k + startRow < 0) continue;
if(l + startCol < 0) continue;
if(k + startRow >= lock.length) continue;
if(l + startCol >= lock.length) continue;
if(key[k][l] == 1 && lock[startRow + k][startCol + l] == 0) {
cnt++;
} else if(key[k][l] == lock[startRow + k][startCol + l]) {
stopflag = true;
break;
}
}
if(stopflag) break;
}
if(cnt == numOfEmpty) return true;
}
}
}
return answer;
}
private int getNumOfEmpty(int[][] arr) {
int numOfEmpty = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j] == 0) numOfEmpty++;
}
}
return numOfEmpty;
}
private int[][] rotate(int[][] key) {
int[][] newKey = new int[key.length][key.length];
for (int i = 0; i < key.length; i++) {
for (int j = 0; j < key.length; j++) {
newKey[i][j] = key[j][key.length - 1 - i];
}
}
return newKey;
}
}