Skip to content

Instantly share code, notes, and snippets.

@niklasjang
Created March 23, 2020 03:35
Show Gist options
  • Select an option

  • Save niklasjang/2fa0b46c78e8fc831b019b807b261fd6 to your computer and use it in GitHub Desktop.

Select an option

Save niklasjang/2fa0b46c78e8fc831b019b807b261fd6 to your computer and use it in GitHub Desktop.
[PS][완전탐색][N자리 K진수]/[BOJ][9663][N-Queen]
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int n=0, k=0;
int map[20][20];
int arr[20];
int ans = 0;
bool visited[20];
bool isPossible(int depth){
if(depth == 1) return true;
int ax=0,ay=0,bx=0,by=0;
for(int i=0; i<depth-1; i++){
// ax = i;
// ay = arr[i];
// bx = depth-1;
// by = arr[depth-1];
// int dx = abs(ax - bx);
// int dy = abs(ay - by);
if(abs(i - depth + 1) ==abs(arr[i] - arr[depth-1])) return false;
}
return true;
}
void recur(int depth){
if(!isPossible(depth)) return;
if(depth == n){
// for(int i=0; i<n; i++){
// cout<<arr[i]<<' ';
// }
// cout<<'\n';
ans++;
return;
}
for(int i=0; i<n; i++){
if(visited[i]) continue;
arr[depth] = i;
visited[i] = true;
recur(depth+1);
visited[i] = false;
}
}
int main (void){
cin.tie(NULL);
ios::sync_with_stdio("false");
cin>> n;
recur(0);
cout<< ans<<'\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment