fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. int n; // MAX = 16
  6. int solution_count = 0;
  7. int solution_count_half = 0;
  8. int solution_count_center = 0;
  9. uint32_t col=0, asc=0, desc=0;
  10.  
  11.  
  12. bool bCenter=false;
  13. void backtrack(int i)
  14. {
  15. int max = n;
  16. if (i==0) max = (n+1)/2;
  17.  
  18. if(i == n) {
  19. if (bCenter)
  20. solution_count_center++;
  21. else
  22. solution_count_half++;
  23. return;
  24. }
  25. uint32_t mc, md, ma;
  26. for(int j=0; j != max; j++) {
  27. if (i==0 && (n%2)==1 && j==max-1)
  28. bCenter=true;
  29. if (col & (mc = 1<<j)) continue;
  30. if (desc & (md = 1 << (i+j))) continue;
  31. if (asc & (ma = 1 << (15+i-j))) continue;
  32. uint32_t oldc = col, oldd = desc, olda = asc;
  33. col |= mc;
  34. desc |= md;
  35. asc |= ma;
  36. backtrack(i+1);
  37. col = oldc;
  38. desc = oldd;
  39. asc = olda;
  40. }
  41. }
  42.  
  43. int main()
  44. {
  45. n=14;
  46.  
  47. backtrack(0);
  48. if (n % 2 == 0)
  49. solution_count = solution_count_half * 2;
  50. else
  51. solution_count = solution_count_half * 2 + solution_count_center;
  52. printf("%d\n", solution_count);
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 1.46s 3460KB
stdin
14
stdout
365596