该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#define MAX_VERTEX_NUM 20
typedef struct{
char vexs[MAX_VERTEX_NUM];// 顶点向量
int AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];// 邻接矩阵
intvexnum, arcnum;// 顶点数、边/弧数
}MGraph;int n,path[100];bool visited[MAX_VERTEX_NUM];
int LocateVex(MGraph G,char v)
{for(int i=0;i
if(G.vexs[i] == v)
return i;
return -1;
}MGraph CreateMDG()
{int i,j,k;
char v1,v2;
MGraph G;
printf("输入定点数 边数");
scanf("%d%d",&G.vexnum, &G.arcnum);
/* IncInfo为0则弧不含其它信息 */
printf("输入顶点名称");
for(i=0;i
{scanf("%c",&G.vexs[i]);printf("%d",i);}for(i=0;i
for(j=0;j
{G.AdjMatrix[i][j]=0;}
printf("输入BIAN");
for(k=0;k
{printf("%d",G.arcnum);
scanf("%c%c",&v1,&v2); /* 输入边v1,v2*/
i=LocateVex(G,v1);
j=LocateVex(G,v2);
G.AdjMatrix[i][j]=G.AdjMatrix[j][i]=1;
}
return G;}
void DFS(MGraph G,char v)
{int j,i;i = LocateVex(G,v);
visited[i]=true;
path[n]=i;
n++;
if(n==G.vexnum)
for(i=0;i
printf("%d",path[i]);
/* 符合条件,输出该简单路径*/
for(j=0; j
if( G.AdjMatrix[i][j]&& !visited[j])
DFS(G,G.vexs[j]);
visited[i]=false; //查找失败时,回溯处理
n--;}void Hamilton(MGraph G)
{int i;
for(i=0;i
visited[i]=false;
n=0;
for(i=0;i
if (!visited[i])
DFS(G,i); }
void main()
{
MGraph G;
G=CreateMDG();
Hamilton(G);
}