#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <limits>
#include <iomanip>
#define N 500
using namespace std;
class Players
{
public:
double x=0,y=0; //当前位置
double tmpmoved,moved; //移动距离
double distance(double tx, double ty) //移动构造函数
{
tmpmoved=sqrt(pow(tx - x, 2) + pow(ty - y, 2));
moved += tmpmoved;
x = tx;
y = ty;
//cout<<">>"<<x<<' '<<y<<endl;
return tmpmoved;
}
};
typedef struct Aixs{
double x,y;
bool minVisited= false;
}aixs;
int cnt=0;
aixs MinD(Players player,aixs arr[],int n)
{
int k=0;
double o, tmp;
tmp = std::numeric_limits<double>::max();
aixs p;
for (int i = 0; i < n; ++i)
{
if(!arr[i].minVisited)
{
o=sqrt(pow(arr[i].x - player.x, 2) + pow(arr[i].y - player.y, 2));
if(o<tmp)
{
tmp=o;
p=arr[i];
k=i;
}
}
}
arr[k].minVisited=true;
cnt++;
//cout<<">>To "<<p.x<<' '<<p.y<<endl;
return p;
}
int main()
{
Players player;
int n;
aixs p,arr[N];
cin>>n;
for (int i = 0; i < n; ++i)
{
cin>>arr[i].x>>arr[i].y;
}
while(cnt!=n)
{
p= MinD(player,arr,n);
player.distance(p.x,p.y);
//cout<<" tmpMoved: "<<player.tmpmoved<<endl;
}
cout<<fixed<<setprecision(2)<<player.moved<<endl;
return 0;
}
我也不知道我这么写属于什么,是不是算法也不清楚..
思路是这样的:
设定从0,0开始,每次寻找距离自己最短的点,移动过去(更新当前点坐标),然后寻找下一个最近点,循环,直到走完所有点...想不清楚还有什么情况回十最近点... 刚开始学c++,不知道哪里有问题,求大佬指教!