#include <stdio.h>
#include <string.h>
#define M 4
int a[M][M]= {0,30,6,4,
30,0,5,10,
6,5,0,20,
4,10,20,0};
int Min_p[M+1]={},temp_Min_p[M+1]={},temp_Min_p2[M+1];
int Min_c=0,i,temp_Min_c=0,temp_Min_c2=0;
condination_t(int I,int k){
int m;
if(I>=M+1){//能遍历到全部节点时
m=0;
Min_c=temp_Min_c;//还原状态;
for(m=0;m<M+1;m++)
Min_p[m]=temp_Min_p[m];
while(m<M+1)
printf("%d ",Min_p[m++]);
return;
}
else{
for(i=0;i<M;++i){//遍历每个节点
if(a[k][i]!=0){
temp_Min_c2=temp_Min_c;//记录回溯时的状态
for(m=0;m<M+1;m++)
temp_Min_p2[m]=temp_Min_p[m];
temp_Min_c+=a[k][i];//探索式添加
if(temp_Min_c<=Min_c){//如果还没有大于最小路径,就添加进路径中。
temp_Min_p[I]=i;
if((I==M-1)&&i!=0) continue;//如果到了最后那个节点时,但是节点回不到开始节点,那就放弃这条路
condination_t(I+1,i);
temp_Min_c=temp_Min_c2;//还原状态;
for(m=0;m<M+1;m++)
temp_Min_p[m]=temp_Min_p2[m];
}
}
}
}
}
void main(){
int j=0;
condination_t(0,0);
while(j<M+1)
printf("%d ",Min_p[j++]);
}
1,18 全部