-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
65 lines (56 loc) · 1.59 KB
/
Solution.java
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
//
// DFS(단체사진 찍기)
// https://programmers.co.kr/learn/courses/30/lessons/1835
// Created by hyungwook on 2021/11/11.
//
public class Solution {
String[] data;
int answer;
char[] person;
boolean[] check;
public int solution(int n, String[] data) {
this.data = data;
check = new boolean[8];
answer = 0;
person = new char[]{'A', 'C', 'F', 'J', 'M', 'N', 'R', 'T'};
dfs(0, "");
return answer;
}
private void dfs(int l, String tmp) {
if (l == 8) {
if (isSatisfy(tmp)) {
answer++;
}
} else {
for (int i = 0; i < 8; i++) {
if (!check[i]) {
check[i] = true;
dfs(l + 1, tmp + person[i]);
check[i] = false;
}
}
}
}
private boolean isSatisfy(String tmp) {
for (String s : data) {
char c1 = s.charAt(0);
char c2 = s.charAt(2);
char op = s.charAt(3);
int reg = s.charAt(4) - '0' + 1;
if (op == '=') {
if (Math.abs(tmp.indexOf(c1) - tmp.indexOf(c2)) != reg) {
return false;
}
} else if (op == '>') {
if (Math.abs(tmp.indexOf(c1) - tmp.indexOf(c2)) <= reg) {
return false;
}
} else {
if (Math.abs(tmp.indexOf(c1) - tmp.indexOf(c2)) >= reg) {
return false;
}
}
}
return true;
}
}