Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- using namespace std;
- ifstream f("e:\\info\\backtracking\\ciclurielementare.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 k,int n)
- { 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 n,int x)
- { for(int i=1;i<=n;i++)
- cout<<st[i]<<" ";
- cout<<x;
- cout<<endl;
- }
- void bt(int k,int st[20],int n,int a[20][20],int x,int y)
- { int as,ev,nr=0;
- k=2;
- st[k]=0;
- while(k>1)
- { as=1;
- ev=0;
- while(as && !ev)
- { as=succesor(st,k,n);
- 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<<"nr="<<nr<<" "<<endl;
- }
- int main()
- { int b,x,y,n,m,st[20],k,a[20][20]={0};
- f>>n>>m;
- citire(a,m);
- afisare(a,n);
- for(x=1;x<=n;x++)
- { cin>>b;
- st[1]=x;
- bt(k,st,n,a,x,y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement