dude2099

Problema colorarii hartilor

Dec 3rd, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int x[10],a[10][10],n,m;
  7.  
  8. void init(int k)
  9. {
  10.     x[k]=0;
  11. }
  12.  
  13. int existaSuccesor(int k)
  14. {
  15.     return x[k]<m;
  16. }
  17.  
  18. int cont(int k)
  19. {
  20.     for(int i=1;i<=k;i++)
  21.         if(a[i][k]==1 && x[i]==x[k])
  22.             return 0;
  23.     return 1;
  24. }
  25.  
  26. int solutie(int k)
  27. {
  28.     return k==n;
  29. }
  30.  
  31. void tipar()
  32. {
  33.     for(int i=1;i<=n;i++)
  34.         switch(x[i])
  35.         {
  36.             case 1: cout<<"Rosu"; break;
  37.             case 2: cout<<"Galben"; break;
  38.             case 3: cout<<"Albastru"; break;
  39.             case 4: cout<<"Mov"; break;
  40.         }
  41.     cout<<endl;
  42. }
  43.  
  44. void back(int k)
  45. {
  46.     init(k);
  47.     while(existaSuccesor(k))
  48.     {
  49.         x[k]=x[k]+1;
  50.         if(cont(k))
  51.             if(solutie(k))
  52.                 tipar();
  53.         else
  54.             back(k+1);
  55.     }
  56. }
  57.  
  58. int main()
  59. {
  60.     ifstream f("intrare.in");
  61.     cout<<"Cate tari sunt: "; f>>n;
  62.     for(int i=1;i<n;i++)
  63.         for(int j=i+1;j<=n;j++)
  64.         {
  65.             cout<<"Tara "<<i<<" este vecina cu tara "<<j<<"."<<endl;
  66.             f>>a[i][j];
  67.             a[j][i]=a[i][j];
  68.         }
  69.     cout<<"Cate culori sunt: "; cin>>m;
  70.     back(1);
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment