Fetch the repository succeeded.
This action will force synchronization from charlieshu/Algorithms, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
using namespace std;
static int n = 0;
double dist(double x1, double y1, double x2, double y2){
return sqrt(pow(x1-x2, 2.0)+pow(y1-y2,2.0));
}
double minDist(double * points, int startPoint, int * usedPath, int usedSize,int n){
double md = 100000, temp;
int newUsedPath[n];
//printf("startPoint=%d, usedSize=%d , n=%d", startPoint, usedSize, n);
for(int i=0;i<n;i++){
// cout<<"check usedPath["<<i<<"]="<<usedPath[i]<<endl;
if(usedPath[i]){
if(usedSize == n-1){
// cout<<"return";
return dist(points[startPoint*2], points[startPoint*2+1], points[i*2], points[i*2+1]);
}else{
memcpy(newUsedPath, usedPath, n);
newUsedPath[i] = 0;
temp = minDist(points, i, newUsedPath, usedSize+1,n)+dist(points[startPoint*2], points[startPoint*2+1], points[i*2], points[i*2+1]);
newUsedPath[i] = 1;
}
if(temp<md){
// cout<<"change md from "<<md<<" to "<<temp<<" when startPoint="<<startPoint<<" endPoint="<<i<<endl;
md = temp;
}
// cout<<"from "<<i<<" to left "<<usedSize-1<<" dist "<<md<<endl;
}
}
return md;
}
int main(){
//1. input
cin>>n;
n++;
double points[n][2];
points[0][0] = 0;
points[0][1] = 0;
int usedPath[n];
for(int i=1;i<n;i++){
cin>>points[i][0]>>points[i][1];
usedPath[i] = 1;
}
//memset(usedPath,1,sizeof(usedPath));
//2. calc
usedPath[0] = 0;
double md = minDist(&points[0][0], 0, &usedPath[0], 1,n);
printf("%.2f",md);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。