fork download
  1. //Распоређивање n дама на шаховској табли
  2. #include <iostream>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int n, permutacija[15];
  8.  
  9. bool dame_se_napadaju()
  10. {
  11. for (int i = 0; i < n; i++)
  12. for (int j = i + 1; j < n; j++)
  13. if (abs(i - j) == abs(permutacija[i] - permutacija[j]))
  14. return true;
  15. return false;
  16. }
  17.  
  18. void prikazi()
  19. {
  20. for (int i = 0; i < n; i++)
  21. cout << permutacija[i] << " ";
  22. cout << '\n';
  23. }
  24.  
  25. int main()
  26. {
  27. ios_base::sync_with_stdio(false);
  28. cin >> n;
  29. for (int i = 0; i < n; i++)
  30. permutacija[i] = i + 1;
  31. if (!dame_se_napadaju())
  32. prikazi();
  33. while (true)
  34. if (next_permutation(permutacija, permutacija + n))
  35. {
  36. if (!dame_se_napadaju())
  37. prikazi();
  38. }
  39. else
  40. break;
  41. return 0;
  42. }
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout