package hg;
public class Hanmierdun {
//结点的个数
public int n =5;
public int b[] = new int[5];
//是否已经经过该结点
public boolean used[] = new boolean[5];
//是否已经全部走过
public boolean cc = false;
public static void main(String args[]){
//汉密尔顿连接图
int a[][]={{0,1,1,1,0},
{1,0,1,0,1},
{1,1,0,1,0},
{1,0,1,0,1},
{0,1,0,1,0}
};
Hanmierdun aa = new Hanmierdun();
aa.dfs(a, 0, 0);
}
private void dfs(int a[][],int count,int m){
//如果所有的结点都已经走过且最后一个结点和开始的结点是相通的
if(count>=this.n&&a[b[count-1]][0]==1){
//输出经过的结点次序
for(int i=0;i<b.length;i++){
System.out.print(b[i]+1);
}
System.out.println();
cc=true;
return;
}
else{
for(int j=0;j<a[m].length;j++){
//如果联通且未经过
if(a[m][j]==1&&used[m]==false){
//设置为已经通过
used[m] = true;
b[count] = m;
count = count+1;
dfs(a,count,j);
//设置为未通过
used[m] = false;
count = count-1;
if(cc){
cc=false;
return;
}
}
}
}
}
}