哈密尔顿回路 C++
来源:互联网 发布:微信html5制作软件 编辑:程序博客网 时间:2024/05/16 23:33
突然记起来我大二写了个哈密尔顿回路..于是共享之...
/**作者: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 0
- [c++,algorithm] 哈密尔顿回路判断
- 哈密尔顿回路
- 哈密尔顿回路
- 哈密尔顿回路 C++
- 哈密尔顿回路问题
- “欧拉回路”与“哈密尔顿回路”
- 欧拉回路和哈密尔顿回路
- POJ 1776 竞赛图的哈密尔顿回路
- SGU 122. The book 哈密尔顿回路 构造
- 竞赛图找哈密尔顿回路(hdu3414)
- 初学回溯,哈密尔顿回路问题的实现
- poj2438 Children's Dining(哈密尔顿回路)
- hdu 4337——poj 2438(哈密尔顿回路求解模板)
- Granny's Bike(哈密尔顿回路--深搜,回溯)
- UVA 216 二维平面的最小哈密尔顿回路
- 图论问题--求哈密尔顿回路的问题
- HDU3414:Tour Route(竞赛图 + 哈密尔顿回路)
- AGC018:Tree and Hamilton Path(dfs & 树 哈密尔顿回路)
- JavaScript基础篇--Undefined与Null
- 猜年龄 - 蓝桥杯
- <%@ include file=""%>,<jsp:include page=""/>
- 一切成功源于积累——20140405 美国非农5分钟k线直至收盘 各货币对表现
- "扫雷外挂" 之C++ 程序篇
- 哈密尔顿回路 C++
- 切面条 - 蓝桥杯
- CImage实现图片缩放
- block的概念及简单用法,通俗易懂
- POJ - 1990 MooFest
- AOV网络与拓扑(一)
- 冒泡排序的三种实现
- Python学习笔记
- S3C2440-CAMIF接口文档解读