Skip to content

Instantly share code, notes, and snippets.

@victoriagjh
Created January 21, 2020 13:14
Show Gist options
  • Save victoriagjh/72fdb8e76d64ea9a3a19e26ecbef99c5 to your computer and use it in GitHub Desktop.
Save victoriagjh/72fdb8e76d64ea9a3a19e26ecbef99c5 to your computer and use it in GitHub Desktop.
너무 오랜만에 풀어서 머리가 안돌아갔다..
#include <iostream>
using namespace std;
int n,map[16];
int res=0;
bool isPossible(int x){
for(int i=1; i<x; i++) {
if(map[x]==map[i] || abs(x-i) == abs(map[x]-map[i])) {
return false;
}
}
return true;
}
void dfs(int x){
if(x==n) {
res++;
return;
}
for(int i=1; i<=n; i++) {
map[x+1]=i;
if(isPossible(x+1)) {
dfs(x+1);
}
map[x+1] = 0;
}
}
int main() {
cin>>n;
for(int i=1; i<=n; i++) {
map[1]=i;
dfs(1);
map[1]=0;
}
cout<<res<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment