哈密尔顿回路 C++

突然记起来我大二写了个哈密尔顿回路..于是共享之...

/**
作者:hwb
时间:2013.3.2-2013.3.6
任务:求出全部的哈密尔顿回路
完成感想:根据一个论文的算法写的,如果有高手可以提出改进的话,请发邮件到我的QQ 409169964
		  PS;中国的论文好多都是抄来抄去,只是名字改了...
*/


#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>
using namespace std;

struct Hami
{
	vector<string> path;
	bool whether;
};

Hami  arr[10][10];
Hami M[10][10];
Hami temp[10][10];

void hami(int );

int main ()
{
	ifstream fin("read.txt",ios::beg);
	if(!fin)
	{
		cout<<"can't find it)";
		return -1;
	}

	int row;
	fin>>row;
	
	string str[10][10];
	string input;
	for(int i=0;i<row;i++)
	{
		for(int j=0;j<row;j++)
		{
			fin>>input;
			if(input=="0")
			{
				str[i][j]="-1";
			}
			else
			{
				string temp,temp2;		
				char ch[10];
				_itoa((i+1),ch,10);
				temp=ch;
				_itoa((j+1),ch,10);
				temp2=ch;
				str[i][j]=temp+temp2;
			}
		}
	}


	for(int i=0;i<row;i++)
	{
		for(int j=0;j<row;j++)
		{
			arr[i][j].path.push_back(str[i][j]);
			arr[i][j].whether=false;
		}
	}
	
	for(int i=0;i<row;i++)
	{
		int judeg=0;
		for(int j=0;j<row;j++)
		{
			if(arr[i][j].path[0]=="-1")
			{
				judeg++;
			}
			if(judeg==row)
			{
				cout<<"没有哈密尔顿回路"<<endl;
				return 0;
			}
		}
	}

	for(int i=0;i<row;i++)
	{
		for(int j=0;j<row;j++)
		{
			if(arr[i][j].path[0]!="-1")
			{
				int length=arr[i][j].path[0].length();
				M[i][j].path.push_back(arr[i][j].path[0].substr(1,length));
			}
			else
			{
				M[i][j].path.push_back("-1");
			}			
			M[i][j].whether=false;
		}
	}

	for(int i=0;i<row;i++)
	{
		for(int j=0;j<row;j++)
		{
			temp[i][j].whether=false;
		}
	}

	hami(row);
	fin.close();
	return 0;
}


void hami(int row)
{
	int k=1;
	for(k;k<row;k++)
	{
		for(int i=0;i<row;i++)
		{
			for(int j=0;j<row;j++)
			{
				bool real=false;
				for(int f=0;f<row;f++)
				{
					string tt="-1";
					const char* jj=M[f][j].path[0].c_str();
					if(arr[i][f].whether==false&&M[f][j].whether==false)
					{
						if(arr[i][f].path[0]=="-1"||M[f][j].path[0]=="-1")
						{
							continue;
						}
						if(arr[i][f].path[0].find(M[f][j].path[0])==M[f][j].path[0].npos)
						{
							tt=arr[i][f].path[0]+M[f][j].path[0];
							vector<string>::iterator it1=temp[i][j].path.begin();
							vector<string>::iterator end1;
							for(it1;it1!=temp[i][j].path.end();)
							{
								if((*it1).length()<=k+1)
								{
									it1=temp[i][j].path.erase(it1);
								}
								else
								{
									it1++;
								}
							}
							temp[i][j].path.push_back(tt);
							it1=temp[i][j].path.begin();
							end1=temp[i][j].path.end();
							int number=0;
							for(it1;it1!=end1;it1++)
							{
								number++;
							}
							if(number>1)
							{
								temp[i][j].whether=true;
							}
							else
							{
								temp[i][j].whether=false;
							}
							real=true;
							continue;
						}
						else if(k==(row-1)&&arr[i][f].path[0][0]==*jj)
						{
							tt=arr[i][f].path[0]+M[f][j].path[0];
							vector<string>::iterator it1=temp[i][j].path.begin();
							for(it1;it1!=temp[i][j].path.end();)
							{
								if((*it1).length()<=k+1)
								{
									it1=temp[i][j].path.erase(it1);
								}
								else
								{
									it1++;
								}
							}							
							temp[i][j].path.push_back(tt);
							vector<string>::iterator it2=temp[i][j].path.begin();
							vector<string>::iterator end2=temp[i][j].path.end();
							int number=0;
							for(it2;it2!=end2;it2++)
							{
								number++;
							}
							if(number>1)
							{
								temp[i][j].whether=true;
							}
							else
							{
								temp[i][j].whether=false;
							}
							real=true;
							continue;
						}
					}
					else if(arr[i][f].whether==true&&M[f][j].whether==false)
					{
						if(M[f][j].path[0]=="-1")
						{
							continue;
						}
						vector<string>::iterator it3=arr[i][f].path.begin();
						for(it3;it3!=arr[i][f].path.end();it3++)
						{
							if((*it3).find(M[f][j].path[0])==M[f][j].path[0].npos)
							{
								tt=(*it3)+M[f][j].path[0];
								if(temp[i][j].path.empty()==false)
								{
									vector<string>::iterator beg1=temp[i][j].path.begin();
									vector<string>::iterator end1=temp[i][j].path.end();
									for(beg1;beg1!=temp[i][j].path.end();)
									{				
										beg1=temp[i][j].path.erase(beg1);										
									}							
								}
								temp[i][j].path.push_back(tt);
								real=true;
								vector<string>::iterator it4=temp[i][j].path.begin();
								vector<string>::iterator end4=temp[i][j].path.end();
								int number=0;
								for(it4;it4!=end4;it4++)
								{
									number++;
								}
								if(number>1)
								{
									temp[i][j].whether=true;
								}
								else
								{
									temp[i][j].whether=false;
								}
							}
							else if(k==(row-1)&&(*it3)[0]==*jj)
							{
								tt=(*it3)+M[f][j].path[0];
								temp[i][j].path.push_back(tt);
								real=true;
								vector<string>::iterator it5=temp[i][j].path.begin();
								vector<string>::iterator end5=temp[i][j].path.end();
								int number=0;
								for(it5;it5!=end5;it5++)
								{
									number++;
								}
								if(number>1)
								{
									temp[i][j].whether=true;
								}
								else
								{
									temp[i][j].whether=false;
								}
							}			
						}
					}					
				}
				if(real==false)
				{					
					if(temp[i][j].path.empty()==true)
					{
						temp[i][j].path.push_back("-1");
						temp[i][j].whether=false;
					}
					else
					{
						vector<string>::iterator beg6=temp[i][j].path.begin();
						vector<string>::iterator end6=temp[i][j].path.end();
						temp[i][j].path.erase(beg6,end6);
						temp[i][j].path.push_back("-1");
						temp[i][j].whether=false;
					}
				}
			}
		}
		for(int i=0;i<row;i++)
		{
			for(int j=0;j<row;j++)
			{
				vector<string>::iterator beg7=arr[i][j].path.begin();
				vector<string>::iterator end7=arr[i][j].path.end();
				arr[i][j].path.erase(beg7,end7);
				vector<string>::iterator it7=temp[i][j].path.begin();
				for(it7;it7!=temp[i][j].path.end();it7++)
				{
					string t=*it7;
					arr[i][j].path.push_back(t);
					if(temp[i][j].whether==true)
					{
						arr[i][j].whether=true;
					}
					else
					{
						arr[i][j].whether=false;
					}
				}
			}
		}
		for(int i=0;i<row;i++)
		{
			int judeg=0;
			for(int j=0;j<row;j++)
			{
				if(arr[i][j].path[0]=="-1")
				{	
					judeg++;
				}
				if(judeg==row)
				{
					cout<<"没有哈密尔顿回路"<<endl;
					return ;
				}
			}
		}
	}
	if(k==row)
	{
		cout<<"哈密尔顿回路是"<<endl;
		for(int i=0;i<row;i++)
		{
			vector<string>::iterator it=temp[i][i].path.begin();
			for(it;it!=temp[i][i].path.end();it++)
			{
				cout<<i<<": ";
				cout<<*it<<endl;
			}
		}
	}
}


。。好久以前写的...


输出图的全部Hamiltonian回路的新算法  

可以看这篇文章,写得不错

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值