-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJW_60062.java
More file actions
57 lines (53 loc) Β· 2.02 KB
/
JW_60062.java
File metadata and controls
57 lines (53 loc) Β· 2.02 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
import java.util.Arrays;
class JW_60062 {
int w, d;
int answer = Integer.MAX_VALUE;
public int solution(int n, int[] weak, int[] dist) {
w = weak.length;
d = dist.length;
Arrays.sort(dist); // 그리λν νμ΄λ₯Ό μν΄ μ λ ¬
judge(0, 0, 0, n, weak, dist);
return answer != Integer.MAX_VALUE ? answer : -1;
}
/*
* @param depth: κΉμ΄(λͺ λͺ
μ μ¬μ©νλκ°)
* @param p: λ§μ§λ§ νμμ΄ λλ μΈλ±μ€
* @param visited: λ°©λ¬Έμ²λ¦¬
*/
private void judge(int depth, int p, int visited, int n, int[] weak, int[] dist) {
// λͺ¨λ μΈλ²½μ μ리 νλ€λ©΄ κ°±μ
if (visited == (1 << w) - 1) {
answer = Math.min(answer, depth);
return;
}
// μ’
λ£ μ‘°κ±΄
// κΉμ΄κ° μ΅λκ±°λ μ΅μ μ μ νμ΄ μλ κ²½μ°
if (depth == d || depth >= answer - 1) {
return;
}
// λ§μ΄ μμ§μ΄λ μΉκ΅¬λΆν° μ ν
int move = dist[d - 1 - depth];
for (int i = p; i < w; i++) {
int next = visited; // λ€μ λ°©λ¬Έ λ°°μ΄
int l = weak[i], r = l + move; // ν΄λΉ μΉκ΅¬κ° νμνλ λ²μ
int idx = i;
// μλ¦¬κ° κ°λ₯ν λ²μκΉμ§ νμ
while (isPossible(l, r, weak[idx], weak[idx] + n)) {
// μ΄λ―Έ μ리λ₯Ό ν κ³³μ΄λΌλ©΄ μ’
λ£
if ((next & (1 << idx)) == 1)
break;
next |= (1 << idx);
idx++;
// μν λ°°μ΄μ΄λ―λ‘ μΈλ±μ€ μ΄κΈ°ν
if (idx >= w)
idx %= w;
}
// λ€μ μΉκ΅¬λ₯Ό μ¬μ©νμ¬ νμ μ§ν
judge(depth + 1, idx, next, n, weak, dist);
}
}
// νμ¬ μμΉλ₯Ό μ리ν μ μλμ§ νμΈνλ ν¨μ
private boolean isPossible(int l, int r, int point, int other) {
return (l <= point && point <= r) || (l <= other) && (other <= r);
}
}