Advertisement
monyca98

cicluri elementare

May 27th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. ifstream f("e:\\info\\backtracking\\ciclurielementare.txt");
  5. void citire(int a[20][20],int m)
  6. { int i,j;
  7. for(int k=1;k<=m;k++)
  8. { f>>i>>j;
  9. a[i][j]=1;
  10. a[j][i]=1;
  11. }
  12. }
  13. void afisare(int a[20][20],int n)
  14. { for(int i=1;i<=n;i++)
  15. { for(int j=1;j<=n;j++)
  16. cout<<a[i][j]<<" ";
  17. cout<<endl;
  18. }
  19. }
  20. int succesor(int st[20],int k,int n)
  21. { if(st[k]<n)
  22. { st[k]++;
  23. return 1;
  24. }
  25. else
  26. return 0;
  27. }
  28. int valid(int st[20],int k,int a[20][20])
  29. { for(int i=1;i<k;i++)
  30. if(st[i]==st[k])
  31. return 0;
  32. if(a[st[k]][st[k-1]]==0 && k>1)
  33. return 0;
  34. return 1;
  35. }
  36. void tipar(int st[20],int n,int x)
  37. { for(int i=1;i<=n;i++)
  38. cout<<st[i]<<" ";
  39. cout<<x;
  40. cout<<endl;
  41. }
  42. void bt(int k,int st[20],int n,int a[20][20],int x,int y)
  43. { int as,ev,nr=0;
  44. k=2;
  45. st[k]=0;
  46. while(k>1)
  47. { as=1;
  48. ev=0;
  49. while(as && !ev)
  50. { as=succesor(st,k,n);
  51. if(as)
  52. ev=valid(st,k,a);
  53. }
  54. if(as)
  55. if(a[st[k]][x]==1 && k>2)
  56. { tipar(st,k,x);
  57. nr++;
  58. }
  59. else
  60. { k++;
  61. st[k]=0;
  62. }
  63. else
  64. k--;
  65. }
  66. cout<<"nr="<<nr<<" "<<endl;
  67. }
  68. int main()
  69. { int b,x,y,n,m,st[20],k,a[20][20]={0};
  70. f>>n>>m;
  71. citire(a,m);
  72. afisare(a,n);
  73. for(x=1;x<=n;x++)
  74. { cin>>b;
  75. st[1]=x;
  76. bt(k,st,n,a,x,y);
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement