哈密顿路(dfs)
#include<iostream> #include<cstring> using namespace std; int a[101][101],n,flag,num; int b[101],d[101]; void print() { flag=1; for(int i=1;i<=n;i++) { cout<<d[i]<<" "; } } void dfs(int k,int num) { b[k]=1; d[num]=k; if(num==n) { print(); cout<<endl; } for(int i=1;i<=n;i++) { if((b[i]==0)&&(a[k][i]==1)) { dfs(i,num+1); } } b[k]=0; } int main() { flag=0; cin>>n; memset(b,0,sizeof(b)); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cin>>a[i][j]; } } for(int i=1;i<=n;i++) { dfs(i,1); } if (flag==0) { cout<<"no road"; } return 0; } //7 //0 1 0 1 1 0 0 //1 0 1 0 1 0 0 //0 1 0 0 0 0 1 //1 0 0 0 0 0 0 //1 1 0 0 0 1 0 //0 0 0 0 1 0 1 //0 0 1 0 0 1 0