这个代码很乱,没来得及简化,
#include<iostream>
#define MAX 1000
#define N 5
using namespace std;
int BOUND=100;
void main()
{
int path[N][N]=
{
{MAX,3,3,2,6},
{3,MAX,7,3,2},
{3,7,MAX,2,5},
{2,3,2,MAX,3},
{6,2,5,3,MAX}
};
int Init=0;
int i=0;
int flag[N]={1,-1,-1,-1,-1};
int array[N+1]={0,-1,-1,-1,-1,-1};
int YY[N]={0,0,0,0,0};
int YY_num=MAX;
int k[N]={0,0,0,0,0};
int total=0;
while(i>=0)
{
while(k[i]<N)
{
if(path[array[i]][k[i]]<BOUND && flag[k[i]]==-1 &&total<BOUND)
{
flag[k[i]]=1;
total=total+path[array[i]][k[i]];
i=i+1;
array[i]=k[i-1];
int pp=0;
while(array[pp]>=0)
{
pp++;
}
if(pp==N)
{
if(total<YY_num)
{
for(int t=0;t<N;t++)
{
YY[t]=array[t];
}
YY_num=total;
}
//cout<<‘\t'<<total;
// cout<<endl;
BOUND=total;
break;
}
}
else
{
k[i]=k[i]+1;
}
}
total=total-path[array[i-1]][array[i]];
flag[array[i]]=-1;
array[i]=-1;
k[i]=0;
i=i-1;
k[i]=k[i]+1;
}
for(int r=0;r<N;r++)
cout<<YY[r]<<“,”;
cout<<‘\t'<<YY_num;
}