很朴实的DFS没有加任何剪枝和优化,0.7sec ~~
code:
/*
ID: yueqiq
LANG: C++
TASK: checker
*/
#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <limits>
#include <vector>
#include <bitset>
#include <string>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <string.h>
#include <iostream>
#include <algorithm>
#define ls rt<<1
#define rs rt<<1|1
#define Si set<int>
#define LL long long
#define pb push_back
#define PS printf(" ")
#define Vi vector<int>
#define LN printf("\n")
#define SD(a) scanf("%d",&a)
#define PD(a) printf("%d",a)
#define SET(a,b) memset(a,b,sizeof(a))
#define FF(i,a) for(int i(0);i<(a);i++)
#define FD(i,a) for(int i(a);i>=(1);i--)
#define FOR(i,a,b) for(int i(a);i<=(b);i++)
#define FOD(i,a,b) for(int i(a);i>=(b);i--)
#define readf freopen("checker.in","r",stdin)
#define writef freopen("checker.out","w",stdout)
const double pi = acos(-1.0);
const int maxn = 50;
const int BigP = 99999999;
const int INF = 99999999;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
using namespace std;
int N,ans;
bool visx[maxn],visy[maxn],vis1[maxn],vis2[maxn];
//vis1 2-2N vis2 1-(2N-1);
//p is 列
int path[21];
int count=0;
bool can(int x,int y){
if(visx[x]) return false;
if(visy[y]) return false;
if(vis1[x+y]) return false;
if(vis2[N-y+x]) return false;
return true;
}
void dfs(int p){
if(p>N){
ans++;
if(ans<=3){
FOR(i,1,N-1) printf("%d ",path[i]);
printf("%d\n",path[N]);
}
return ;
}
FOR(i,1,N){
if(can(i,p)){
visx[i]=true;
visy[p]=true;
vis1[i+p]=true;
vis2[N-p+i]=true;
path[p]=i;
dfs(p+1);
visx[i]=false;
visy[p]=false;
vis1[i+p]=false;
vis2[N-p+i]=false;
}
}
}
int main(){
readf;
writef;
SD(N);
dfs(1);
PD(ans);LN;
return 0;
}