Advertisement
edutedu

colorarea hartilor

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