https://programmers.co.kr/learn/courses/30/lessons/1835
import java.util.*;
class Solution {
static char[] friends = {'A','C','F','J','M','N','R','T'};
static Stack<Character> st = new Stack<>();
static boolean visit[];
static String restrict[];
static int count;
public int solution(int n, String[] data) {
count=0;
visit = new boolean[8];
restrict = data;
Arrays.fill(visit,false);
for(int i=0;i<8;i++){
dfs(i,1);
}
return count;
}
public void dfs(int start, int depth){
st.push(friends[start]);
if(depth==8){
if(check()){
count++;
}
st.pop();
return;
}
visit[start] = true;
for(int i=0;i<8;i++){
if(!visit[i]){
dfs(i,depth+1);
visit[i] = false;
}
}
visit[start] = false;
st.pop();
}
public boolean check(){
for(String t : restrict){
if(t.charAt(3)=='='){
if(Integer.parseInt(t.charAt(4)+"")+1!=Math.abs(st.indexOf(t.charAt(0))-st.indexOf(t.charAt(2)))){
return false;
}
}
else if(t.charAt(3)=='<'){
if(Integer.parseInt(t.charAt(4)+"")+1<=Math.abs(st.indexOf(t.charAt(0))-st.indexOf(t.charAt(2)))){
return false;
}
}
else{
if(Integer.parseInt(t.charAt(4)+"")+1>=Math.abs(st.indexOf(t.charAt(0))-st.indexOf(t.charAt(2)))){
return false;
}
}
}
return true;
}
}