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