Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- //graf neorientat cu n noduri si m muchii.
- using namespace std;
- int a[30][30], n, m, viz[30], L[30], C[30], k, x, y;
- void citire()
- {
- ifstream f("graf.in");
- int b, c;
- f>>n>>m;
- while(f>>b>>c)
- {
- a[c][b]=a[b][c]=1;
- }
- f.close();
- }
- void lant_x_y(int z, int pas)
- {
- int i, j;
- if(z==y)
- {
- for(j=1; j<pas; j++)
- {
- cout<<L[j]<<" ";
- }
- cout<<endl;
- }
- else
- {
- for(i=1; i<=n; i++)
- {
- if(a[z][i]==1 && !viz[i])
- {
- L[pas]=i;
- viz[i]=1;
- lant_x_y(i,pas+1);
- viz[i]=0;
- }
- }
- }
- }
- void lant_k(int z, int pas)
- {
- int i, j;
- if(pas==k+2)
- {
- for(j=1; j<pas; j++)
- {
- cout<<L[j]<<" ";
- }
- cout<<endl;
- }
- else
- {
- for(i=1; i<=n; i++)
- {
- if(a[z][i]==1 && !viz[i])
- {
- L[pas]=i;
- viz[i]=1;
- lant_k(i,pas+1);
- viz[i]=0;
- }
- }
- }
- }
- void ciclu_x(int z, int pas)
- {
- int i, j;
- if(a[z][C[1]] && pas>3)
- {
- for(j=1; j<pas; j++)
- {
- cout<<C[j]<<" ";
- }
- cout<<C[1];
- cout<<endl;
- }
- else
- {
- for(i=1; i<=n; i++)
- {
- if(a[z][i]==1 && !viz[i])
- {
- C[pas]=i;
- viz[i]=1;
- ciclu_x(i,pas+1);
- viz[i]=0;
- }
- }
- }
- }
- void ciclu_k(int z, int pas)
- {
- int i, j;
- if(a[z][C[1]] && pas==k+1)
- {
- for(j=1; j<pas; j++)
- {
- cout<<C[j]<<" ";
- }
- cout<<C[1];
- cout<<endl;
- }
- else
- {
- for(i=1; i<=n; i++)
- {
- if(a[z][i]==1 && !viz[i])
- {
- C[pas]=i;
- viz[i]=1;
- ciclu_k(i,pas+1);
- viz[i]=0;
- }
- }
- }
- }
- void ciclu_ham(int z, int pas)
- {
- int i, j;
- if(a[z][C[1]] && pas==n+1)
- {
- for(j=1; j<pas; j++)
- {
- cout<<C[j]<<" ";
- }
- cout<<C[1];
- cout<<endl;
- }
- else
- {
- for(i=1; i<=n; i++)
- {
- if(a[z][i]==1 && !viz[i])
- {
- C[pas]=i;
- viz[i]=1;
- ciclu_ham(i,pas+1);
- viz[i]=0;
- }
- }
- }
- }
- int main()
- {
- citire();
- /*cin>>x>>y;
- viz[x]=1;
- L[1]=x;
- lant_x_y(x,2);
- viz[x]=0;
- cout<<"Introduceti lungimea lantului ";
- cin>>k;
- for(int i=1; i<=n; i++)
- {
- viz[i]=1;
- L[1]=i;
- lant_k(i, 2);
- viz[i]=0;
- }
- cout<<"Introduceti primul nod ";
- cin>>x;
- viz[x]=1;
- L[1]=x;
- ciclu_x(x, 2);
- viz[x]=0;
- cin>>k;
- for(int i=1; i<=n; i++)
- {
- viz[i]=1;
- C[1]=i;
- ciclu_k(i,2);
- viz[i]=0;
- }*/
- cin>>x;
- viz[x]=1;
- C[1]=x;
- ciclu_ham(x,2);
- viz[1]=0;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement