import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int[] arr;
static int N;
static int de = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
N = Integer.parseInt(str);
arr = new int[N];
dfs(0);
System.out.println(de);
}
public static void dfs(int depth) {
// dfs 재귀 함수로 계속 호출되면서 depth가 올라갈 때 depth==N면 정답인 경우의 수
if(depth == N) {
de++;
return;
}
// 반복문 돌리면서, dfs 재귀 호출(possible에 부합할 시)
for(int i = 0 ; i < N; i++) {
arr[depth] = i;
if(possible(depth)) {
dfs(depth+1);
}
}
}
// 이전의 퀸들과 대각선, 같은 행에 없음을 check
public static boolean possible(int col) {
for(int i = 0 ; i < col ; i++) {
if(arr[i]==arr[col]) {
return false;
}
else if(Math.abs(col-i) == Math.abs(arr[col]-arr[i])) {
return false;
}
}
return true;
}
}
얻어갈 점: