Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- typedef vector<bool> VB;
- typedef vector<VB> VVB;
- typedef vector<int> VI;
- void escriu(VI& cami){
- cout<<"(";
- for(int i=0;i<cami.size();++i){
- if(i>0)cout<<",";
- cout<<cami[i];
- }
- cout<<")"<<endl;
- }
- void rec(VB& usat, VVB& m, VI& cami, int p, int maxp, int num, bool& trobat){
- if(p==maxp+1){
- escriu(cami);
- trobat=true;
- return;
- }
- for(int j=0;j<m.size();++j){
- if(m[num][j] and not usat[j]){
- cami[p]=j+1;
- usat[j]=1;
- rec(usat,m,cami,p+1,maxp,j,trobat);
- usat[j]=0;
- }
- }
- }
- int main(){
- int n,p;
- bool fi=false;
- while(not fi){
- cin>>n>>p;
- VVB m(n,VB(n));
- int k;
- for(int i=0;i<n;++i)
- for(int j=0;j<n;++j){
- cin>>k;
- m[i][j]=k;
- }
- VI cami(p+1);
- VB usat(n);
- usat[0]=1;
- cami[0]=1;
- bool trobat=false;
- rec(usat,m,cami,1,p,0,trobat);
- if(not trobat)cout<<"no walk of length "<<p;
- if(cin>>n and n==-9999)cout<<endl;
- else fi=true;
- }
- }
Add Comment
Please, Sign In to add comment