Advertisement
Mattia-Iojica

Aranjarea damelor

Jan 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int st[50], n;
  5.  
  6. int abs(int x)
  7. {
  8. if(x>0)
  9. return x;
  10. else
  11. return -x;
  12. }
  13.  
  14. int Valid(int p)
  15. {
  16. for(int i=1;i<p;i++)
  17. if(st[i]==st[p] || abs(st[p]-st[i])==abs(p-i))
  18. return 0;
  19. return 1;
  20. }
  21.  
  22. int Solutie(int p)
  23. {
  24. return n==p;
  25. }
  26.  
  27. void Tipar()
  28. {
  29. for(int i=1;i<=n;i++)
  30. cout<<st[i]<<" ";
  31. cout<<endl;
  32. }
  33.  
  34. void bkt(int p)
  35. {
  36. for(int i=1;i<=n;i++)
  37. {
  38. st[p]=i;
  39. if (Valid(p))
  40. {
  41. if (Solutie(p))
  42. Tipar();
  43. else
  44. bkt(p+1);
  45. }
  46. }
  47. }
  48.  
  49. int main()
  50. {
  51. cin>>n;
  52. bkt(1);
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement