rt,不是行末空格或ans未清零,帮助必关
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,k,g[505][505],ans;
int plan[25];
bool vis[25];
void search(int x,int step)
{
plan[step]=x;
if(x==k)
{
for(int i=1;i<=step-1;i++) cout<<plan[i]<<" ";
cout<<plan[step];
cout<<endl;
ans++;
return;
}
for(int i=1;i<=n;i++)
{
if(g[x][i]==1&&vis[i]==false)
{
vis[i]=true;
search(i,step+1);
vis[i]=false;
}
}
}
int main(){
int t=0;
while(scanf("%d",&k)==1)
{
ans=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=500;i++)
for(int j=1;j<=500;j++)
g[i][j]=0;
t++;
while(true)
{
int x,y;
scanf("%d%d",&x,&y);
if(x==0&&y==0) break;
g[x][y]=g[y][x]=1;
n=max(n,max(x,y));
}
vis[1]=true;
printf("CASE %d:\n",t);
search(1,1);
printf("There are %d routes from the firestation to streetcorner %d.\n",ans,k);
return 0;
}
}