#include <iostream>
using namespace std;
int n, answer = 0;
int vis[15]{ 0 };
// 유망한지 체크
bool Check(int cnt)
{
for (int i = 0; i < cnt; i++)
{
auto vis1 = vis[cnt], vis2 = vis[i];
if (vis1 == vis2 ||
cnt - i == abs(vis1 - vis2))
return 0;
}
return 1;
}
void BackTracking(int cnt)
{
if (cnt == n)
{
answer++;
return;
}
for (int i = 0; i < n; i++)
{
vis[cnt] = i;
if (Check(cnt))
BackTracking(cnt + 1);
}
}
int main()
{
cin >> n;
BackTracking(0);
cout << answer;
}
참고 : https://velog.io/@hhj3258/%EB%B0%B1%EC%A4%80C%EB%B0%B1%ED%8A%B8%EB%9E%98%ED%82%B9-9663%EB%B2%88-N-Queen