fork download
  1. #include <iostream>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. int N;
  6. bool d[16]={0};
  7. bool d1[32]={0};
  8. bool d2[32]={0};
  9.  
  10. ll check(int i, int cnt) {
  11. int x=i/N;
  12. int y=i%N;
  13. ll res=0;
  14.  
  15. if (cnt<=x-1)
  16. return 0;
  17. if (i>=N*N || cnt==N)
  18. return cnt==N;
  19.  
  20. if (d[y] || d1[x+y] || d2[x-y+N-1])
  21. return check(i+1, cnt);
  22.  
  23. d[y] = d1[x+y] = d2[x-y+N-1] = 1;
  24. res += check((x+1)*N, cnt+1);
  25. d[y] = d1[x+y] = d2[x-y+N-1] = 0;
  26.  
  27. res += check(i+1, cnt);
  28.  
  29. return res;
  30. }
  31.  
  32. int main() {
  33. cin.tie(0)->sync_with_stdio(0);
  34. cin >> N;
  35. cout << check(0, 0);
  36. }
Success #stdin #stdout 0s 5272KB
stdin
8
stdout
92