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
| private static final int PEOPLE_COUNT = 8; private static final String[] PEOPLE_NAMES = {"A", "C", "F", "J", "M", "N", "R", "T"};
private static int answer; public int solution(int n, String[] data) { answer = 0; perm("", 0, new boolean[8], data);
return this.answer; }
private void perm(String perm, int depth, boolean[] isVisit, String[] data) { if (depth == PEOPLE_COUNT) {
if (isCondition(perm, data)) { answer++; } }
for (int i = 0; i < PEOPLE_COUNT; i++) { if (!isVisit[i]) { isVisit[i] = true; perm(perm + PEOPLE_NAMES[i], depth + 1, isVisit, data); isVisit[i] = false; } } }
private boolean isCondition(String perm, String[] data) { boolean isCondition = true;
for (int i = 0; i < data.length; i++) { char[] tochar = data[i].toCharArray(); int people1 = perm.indexOf(tochar[0]); int people2 = perm.indexOf(tochar[2]);
int interval = people1 > people2 ? people1 - people2 - 1 : people2 - people1 - 1;
int requestInterval = tochar[4] - '0'; if (tochar[3] == '=' && interval != requestInterval) { isCondition = false; break; }
if (tochar[3] == '<' && interval >= requestInterval) { isCondition = false; break; }
if (tochar[3] == '>' && interval <= requestInterval) { isCondition = false; break; } }
return isCondition; }
|