⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 out< "please input the number of the nodes"<<endl cin>>nodesNum cout<<"pl
💻
字号:
#include<iostream>
#include<cstdlib>
using namespace std;

int x[101];
bool graph[101][101];


bool NextValueAbideByRule( int k,int nodeNum)
{
   
	int preNode;
	int currentNode;
	int i;
	while(1)
	{
		x[k] = div( (x[k]+1),(nodeNum + 1)).rem;
		if( x[k] == 0)   //如果找不到合法的结点
		{
			return false;
		}
		if( k == 1)
			return true;
		//接下来x[k]检查合法性
		if( k > 1)
		{
			preNode = x[k-1];
			currentNode = x[k];

			for( i =1; i < k;i++) //判断是否结点重复
			{
				if(x[k] == x[i])
					break;
			}//for
			if( i < k) continue;

			if( !graph[preNode][currentNode]) //判断是否有边与前一结点相连
				continue;
          	return true;
		}
		

	}//while
}

void OutPut(int nodeNum)
{
	int i;
	cout<<"--------------------"<<endl;
	for(i = 1; i<=nodeNum; i++)
	{
		cout<<x[i]<<"   ";
	}
	cout<<endl;

}

void HamiltonCicle( int nodeNum)
{
	int k =1;
	int firstNode;
	int currentNode;
	int num = 0;

	while( k > 0 )
	{
		if(NextValueAbideByRule( k,nodeNum))
		{
			if( k == nodeNum)
			{
			      firstNode = x[1];
	              currentNode = x[k];
				if(graph[firstNode][currentNode])//如果最后一个结点和第一个结点有边相连接
				{
				    num++;
					OutPut(nodeNum);
				}
			}
			else
				k++;
		}
		else
			k--;
	}
	if(num == 0)
		cout<<"cannot find a hamiltonCircle"<<endl;
}

void main()
{
	
   int 	nodesNum = 5;
	graph[1][1]=0; graph[1][2]=1;  graph[1][3]=0; graph[1][4]=1;  graph[1][5]=1;
	graph[2][1]=1; graph[2][2]=0;  graph[2][3]=1; graph[2][4]=1;  graph[2][5]=1;
	graph[3][1]=0; graph[3][2]=1;  graph[3][3]=0; graph[3][4]=1;  graph[3][5]=0;
	graph[4][1]=1; graph[4][2]=1;  graph[4][3]=1; graph[4][4]=0;  graph[4][5]=0;
	graph[5][1]=1; graph[5][2]=1;  graph[5][3]=0; graph[5][4]=0;  graph[5][5]=0;
	/*int i,j;
	int nodesNum;
	cout<<"please input the number of the nodes"<<endl;
	cin>>nodesNum;
	cout<<"please input the graph"<<endl;
	for( i = 1; i<=nodesNum; i++)
		for( j = 1; j <= nodesNum; j++)
			  cin>>graph[i][j];*/

    HamiltonCicle(nodesNum);
	cout<<"inputing ends"<<endl;
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -