dude2099

PROBLEMA DAMELOR : ITERATIV

Nov 25th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 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. int main()
  35. {
  36.     k=1;
  37.     cout<<"n="; cin>>n;
  38.     init(1);
  39.     while(k>0)
  40.     {
  41.         if(existaSuccesor(k))
  42.         {
  43.             x[k]++;
  44.             if(cont(k))
  45.             {
  46.                 if(solutie(k))
  47.                     tipar();
  48.                 else
  49.                 {
  50.                     k++;
  51.                     init(k);
  52.                 }
  53.             }
  54.         } else {
  55.             k--;
  56.         }
  57.     }
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment