Code: Select all
#include <stdio.h>
#include <math.h>
#define sz 20
long par[sz];
long n,vertex,count;
long mat[sz][sz];
long visit[sz],ans[sz];
bool flag;
void DfsVisit(long node)
{
long i;
visit[node]=1;
if(n+1==count)
{
printf("(1,");
flag=false;
for(i=1;i<=n;i++)
{
printf("%ld", ans[i]);
if(i<n)
{
printf(",");
}
}
printf(")\n");
return;
}
for(i=1;i<=vertex;i++)
{
if(mat[node][i]==1)
{
if(visit[i]==0)
{
ans[count]=i;
count++;
DfsVisit(i);
visit[i]=0;
count--;
}
}
}
return;
}
void Dfs()
{
long i;
for(i=1;i<=vertex;i++)
{
par[i]=0;
visit[i]=0;
}
DfsVisit(1);
}
int main()
{
freopen("677.txt", "w", stdout);
long i,j;
bool tag=false;
while(scanf("%ld %ld", &vertex, &n)==2)
{
flag=true;
if(tag==true)
{
printf("\n");
}
tag=true;
count=1;
for(i=1;i<=vertex;i++)
{
for(j=1;j<=vertex;j++)
{
mat[i][j]=0;
}
}
for(i=1;i<=vertex;i++)
{
for(j=1;j<=vertex;j++)
{
scanf("%ld", &mat[i][j]);
}
}
Dfs();
if(flag==true)
{
printf("no walk of length n\n");
}
if(scanf("%ld", &i)!=1)
{
break;
}
}
return 0;
}