forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4.java
More file actions
67 lines (62 loc) ยท 2.3 KB
/
4.java
File metadata and controls
67 lines (62 loc) ยท 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;
class Solution {
// 2์ฐจ์ ๋ฆฌ์คํธ 90๋ ํ์ ํ๊ธฐ
public static int[][] rotateMatrixBy90Degree(int[][] a) {
int n = a.length;
int m = a[0].length;
int[][] result = new int[n][m]; // ๊ฒฐ๊ณผ ๋ฆฌ์คํธ
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
result[j][n - i - 1] = a[i][j];
}
}
return result;
}
// ์๋ฌผ์ ์ ์ค๊ฐ ๋ถ๋ถ์ด ๋ชจ๋ 1์ธ์ง ํ์ธ
public static boolean check(int[][] newLock) {
int lockLength = newLock.length / 3;
for (int i = lockLength; i < lockLength * 2; i++) {
for (int j = lockLength; j < lockLength * 2; j++) {
if (newLock[i][j] != 1) {
return false;
}
}
}
return true;
}
public boolean solution(int[][] key, int[][] lock) {
int n = lock.length;
int m = key.length;
// ์๋ฌผ์ ์ ํฌ๊ธฐ๋ฅผ ๊ธฐ์กด์ 3๋ฐฐ๋ก ๋ณํ
int[][] newLock = new int[n * 3][n * 3];
// ์๋ก์ด ์๋ฌผ์ ์ ์ค์ ๋ถ๋ถ์ ๊ธฐ์กด์ ์๋ฌผ์ ๋ฃ๊ธฐ
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
newLock[i + n][j + n] = lock[i][j];
}
}
// 4๊ฐ์ง ๋ฐฉํฅ์ ๋ํด์ ํ์ธ
for (int rotation = 0; rotation < 4; rotation++) {
key = rotateMatrixBy90Degree(key); // ์ด์ ํ์
for (int x = 0; x < n * 2; x++) {
for (int y = 0; y < n * 2; y++) {
// ์๋ฌผ์ ์ ์ด์ ๋ฅผ ๋ผ์ ๋ฃ๊ธฐ
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
newLock[x + i][y + j] += key[i][j];
}
}
// ์๋ก์ด ์๋ฌผ์ ์ ์ด์ ๊ฐ ์ ํํ ๋ค์ด ๋ง๋์ง ๊ฒ์ฌ
if (check(newLock)) return true;
// ์๋ฌผ์ ์์ ์ด์ ๋ฅผ ๋ค์ ๋นผ๊ธฐ
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
newLock[x + i][y + j] -= key[i][j];
}
}
}
}
}
return false;
}
}