fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. const int n = 4;
  7. int col[n]; int diag1[2*n+1]; int diag2[2*n+1];
  8. int ans = 0;
  9. int count = 0;
  10.  
  11. void search(int y)
  12. {
  13. if (y == n)
  14. {
  15. ans++;
  16. return;
  17. }
  18. for (int x = 0; x < n; x++)
  19. {
  20. if (col[x] || diag1[x + y] || diag2[x - y + n - 1])continue;
  21. col[x] = diag1[x + y] = diag2[x - y + n - 1] = 1;
  22. search(y + 1);
  23. col[x] = diag1[x + y] = diag2[x - y + n - 1] = 0;
  24. }
  25. }
  26.  
  27.  
  28. int main()
  29. {
  30. search(0);
  31. cout << ans << endl;
  32. }
Success #stdin #stdout 0s 4304KB
stdin
Standard input is empty
stdout
2