dude2099

PROBLEMA DAMELOR : RECURSIV

Nov 25th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int x[100],n,k;
  7.  
  8. void init(int k)
  9. {
  10.     x[k]=0;
  11. }
  12. int existaSuccesor(int k)
  13. {
  14.     return x[k]<n;
  15. }
  16. int solutie(int k)
  17. {
  18.     return k==n;
  19. }
  20. void tipar()
  21. {
  22.     for(int k=1;k<=n;k++)
  23.         cout<<"Dama "<<k<<" pe coloana "<<x[k]<<endl;
  24.     cout<<endl;
  25. }
  26. int cont(int k)
  27. {
  28.     for(int i=1;i<k;i++)
  29.         if(x[k]==x[i] || (k-i==abs(x[k]-x[i])))
  30.             return 0;
  31.     return 1;
  32. }
  33.  
  34. void back(int k)
  35. {
  36.     init(k);
  37.     while(existaSuccesor(k))
  38.     {
  39.         x[k]++;
  40.         if(cont(k))
  41.         {
  42.             if(solutie(k))
  43.                 tipar();
  44.             else
  45.                 back(k+1);
  46.         }
  47.     }
  48. }
  49.  
  50. int main()
  51. {
  52.     k=1;
  53.     cout<<"n="; cin>>n;
  54.     back(1);
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment