#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");
int a[8][8]={{0,1,0,1,0,1,0,0},
			{1,0,1,0,0,0,1,0},
			{0,1,0,1,0,0,0,1},
			{1,0,1,0,1,0,0,0},
			{0,0,0,1,0,1,0,1},
			{1,0,0,0,1,0,1,0},
			{0,1,0,0,0,1,0,1},
			{0,0,1,0,1,0,1,0}};
int node,start,index,check[30],cycle[30];

void hamilton(int i,int index){
	cycle[index]=i;
	check[i]=1;
	if(index == node-1 && a[i][start]==1){
		for(int j=0;j<node;j++)
			cout << cycle[j]+1 << " ";
		cout << endl;
	}
	else{
		for(int j=0;j<node;j++){
			if(a[i][j]==1 && check[j]==0){
				hamilton(j,index+1);
				check[j]=0;
			}
		}
	}
}
void main(){
	node=8; start=0;
	hamilton(start,0);
}

+ Recent posts