Advertisement
Benchea_Paul_Catalin

Colorarea hartilor recursiv

Jan 16th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. //COLORAREA HARTILOR
  2. #include <iostream>
  3. #include <fstream>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. ifstream fin("adiacenta1.txt");
  8.  
  9. int A[50][50], st[50], n;
  10.  
  11. void Citire(int &n)
  12. {
  13.     int i, j;
  14.     fin >> n;
  15.  
  16.     while (fin >> i >> j)
  17.     {
  18.         A[i][j] = A[j][i] = 1;
  19.     }
  20.     fin.close();
  21. }
  22.  
  23.  
  24. int Solutie(int k)
  25. {
  26.     return n == k;
  27. }
  28.  
  29. void Tipar()
  30. {
  31.     for (int i = 1; i <= n; i++)
  32.         cout << st[i] << " ";
  33.     cout << "\n";
  34. }
  35.  
  36.  
  37. int Valid(int k)
  38. {
  39.     for (int i = 1; i < k; i++)
  40.         if (st[i] == st[k] && A[i][k]==1 )
  41.             return 0;
  42.  
  43.     return 1;
  44. }
  45.  
  46.  
  47. void bkt(int k)
  48. {
  49.     for (int i = 1; i <= 4; i++)
  50.     {
  51.         st[k] = i;
  52.         if (Valid(k))
  53.         {
  54.             if (Solutie(k))
  55.                 Tipar();
  56.             else
  57.                 bkt(k + 1);
  58.         }
  59.     }
  60. }
  61.  
  62.  
  63. int main()
  64. {
  65.     Citire(n);
  66.  
  67.  
  68.  
  69.    
  70.     bkt(1);
  71.  
  72.     cout << "\n";
  73.     system("pause");
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement