Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<fstream.h>
- ifstream f("d:\\elev\\mat.txt");
- void citire(int a[20][20],int m)
- { int i,j;
- for(int k=1;k<=m;k++)
- { f>>i>>j;
- a[i][j]=1;
- a[j][i]=1;
- }
- }
- void afisare(int a[20][20],int n)
- { for(int i=1;i<=n;i++)
- { for(int j=1;j<=n;j++)
- cout<<a[i][j]<<" ";
- cout<<endl;
- }
- }
- int succesor(int st[20],int n,int k)
- { if(st[k]<n)
- { st[k]++;
- return 1;}
- else
- return 0;
- }
- int valid(int st[20],int k,int a[20][20])
- {
- for(int i=1;i<k;i++)
- if(st[i]==st[k])
- return 0;
- if(a[st[k]][st[k-1]]==0 && k>1)
- return 0;
- return 1;
- }
- void tipar(int st[20], int k,int x)
- { for(int i=1;i<=k;i++)
- cout<<st[i]<<" ";
- cout<<x;
- cout<<endl;
- }
- void bt(int st[20],int n,int k,int a[20][20],int x)
- { int nr=0,as,ev;
- k=2;
- st[k]=0;
- while(k>1)
- { as=1;
- ev=0;
- while(as && !ev)
- { as=succesor(st,n,k);
- if(as)ev=valid(st,k,a);}
- if(as)
- if(a[st[k]][x]==1 && k>2 )
- { tipar(st,k,x);
- nr++;}
- else
- { k++;
- st[k]=0;}
- else
- k--;
- }
- cout<<"numarul de solutii sunt "<<nr<<" ";
- cout<<endl;
- }
- void main ()
- {int st[20],n,k,a[20][20],x,y,b,m;
- f>>n;
- f>>m;
- citire(a,m);
- afisare(a,n);
- for(x=1;x<=n;x++)
- {cout<<endl;
- cin>>b;
- st[1]=x;
- bt(st,n,k,a,x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement