从第一个点出发 枚举剩下的点,输出其中最接近第一个点的那个点
再以这个点为第一个点继续枚举
最后输出距离
直接暴力做了 最大的数据是8!
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<string.h>
#include<string>
using namespace std;
int con[1050][1050];
int n,x,y,flag;
float sum = 0;
void dfs(int x1,int y1){
int x2, y2,t;
float minh = 9999999999;
float len = 0;
for (int i = 1; i < n; i++){
if (con[i][0] ==0 && con[i][1] == 0)
continue;
len = sqrt((x1 - con[i][0])*(x1 - con[i][0]) + (y1- con[i][1])*(y1 - con[i][1]));
if (len < minh){
minh = len;
x2 = con[i][0];
y2 = con[i][1];
t = i;
}
}
minh += 16;
printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2f feet.\n",x1,y1,x2,y2,minh);
sum += minh;
con[t][0] = 0; con[t][1] = 0;
flag++;
if (flag<n)
dfs(x2,y2);
}
int main(){
int t = 1;
while (scanf_s("%d", &n) && n){
flag = 1;
memset(con,0,sizeof(con));
sum = 0;
for (int i = 0; i < n; i++)
//scanf_s("%d%d", &con[i][0], &con[i][1]);
cin >> con[i][0]>> con[i][1];
x = con[0][0]; y = con[0][1];
con[0][0] = 0; con[0][1] = 0;
printf("**********************************************************\n");
printf("Network #%d\n",t++);
dfs(x,y);
printf("Number of feet of cable required is %.2f.\n",sum);
printf("**********************************************************\n");
}
return 0;
}