Advertisement
AlexandruDu

PROBLEMA ELEVI BACKTRACKING

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